最小起订量 Returns 方法 returns 空

Moq Returns method returns null

我是 Moq 的新手,正在尝试让我的模拟成为 return ASP.NET MVC 中的一个值。文档 here。代码:

mock = new Mock<IRepository<Story>>();

mock.Setup(x => x.GetById( It.Is<int>( i => i==10 ) ))
    .Returns(It.Is<Story>((Story story) => story.Id == 10 && story.Hits == 0));

storiesController = new StoriesController(mock.Object);

ViewResult result = storiesController.Details(10) as ViewResult;

Details方法调用storyRepository.GetById(id)

并且此测试失败:Assert.IsNotNull(result); 因为 GetById 方法 return 为空。

我做错了什么?

        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Story story = storyRepository.GetById(id);
            if (story == null)
            {
                return HttpNotFound();
            }
            story.Hits++; // TODO!
            storyRepository.Update(story);
            storyRepository.Save();
            return View(story);
        }

这是详细信息方法。在调试模式下,一旦我跨过调用的 GetById 方法,我就会看到获取的 Story 为空。

它发生是因为 Returns 结果不是对 :

的断言更改
mock.Setup(x => x.GetById(10) ))
    .Returns(new Story {Id=10 });