使用 NUnit 和 MVC Contrib 进行首次测试时需要指针

发布于 2024-10-06 15:19:43 字数 1722 浏览 2 评论 0原文

我正在使用 ASP.NET MVC 2、NUnit、Moq 和 MVC Contrib。我已经编写了我的第一个单元测试,我对这个测试有几个问题。我的情况是我有一个索引视图。在此视图中,我有一个显示所有新闻项目的网格。

这是我的 INewsRepository 类:

public interface INewsRepository
{
   IEnumerable<News> FindAll();
}

我的测试类和测试方法:

public class NewsControllerTest :TestControllerBuilder
{
   private Mock<INewsRepository> mockNewsRepository;
   private NewsController newsController;

   [SetUp]
   public void Init()
   {
      mockNewsRepository = new Mock<INewsRepository>();
      newsController = new NewsController(mockNewsRepository.Object);
      InitializeController(newsController);
   }

   [Test]
   public void NewsController_Index()
   {
      // Arrange
      var news = new Mock<IEnumerable<News>>();
      mockNewsRepository.Setup(r => r.FindAll()).Returns(news.Object).Verifiable();

      // Act
      ActionResult actual = newsController.Index();

      // Assert
      mockNewsRepository.Verify();
      actual
         .AssertViewRendered()
         .ForView("Index")
         .WithViewData<News[]>()
         .ShouldBe(news);
   }
}

我的观点:

public ActionResult Index()
{
   FakeNewsRepository fakeNewsRepository = new FakeNewsRepository();
   IEnumerable<News> news = fakeNewsRepository.FindAll();
   return View(news);
}

我需要一些关于我的做法的指示。我的方向正确吗?我应该添加什么,我应该省略什么?我想做单元测试,是否将其与集成测试混合在一起?任何额外的建议将不胜感激。

当我在 NUnit GUI 控制台中运行此测试时,我收到一个错误,但我不确定这意味着什么:

MyProject.Web.Tests.Controllers.NewsControllerTest.NewsController_Index:
Moq.MockVerificationException : The following setups were not matched:
INewsRepository r => r.FindAll()

I'm using ASP.NET MVC 2, NUnit, Moq and MVC Contrib. I have written my first unit test ever, and I have a couple of questions regarding this test. My scenario is that I have an Index view. On this view I have a grid that displays all the news items.

Here is my INewsRepository class:

public interface INewsRepository
{
   IEnumerable<News> FindAll();
}

My test class with the test method:

public class NewsControllerTest :TestControllerBuilder
{
   private Mock<INewsRepository> mockNewsRepository;
   private NewsController newsController;

   [SetUp]
   public void Init()
   {
      mockNewsRepository = new Mock<INewsRepository>();
      newsController = new NewsController(mockNewsRepository.Object);
      InitializeController(newsController);
   }

   [Test]
   public void NewsController_Index()
   {
      // Arrange
      var news = new Mock<IEnumerable<News>>();
      mockNewsRepository.Setup(r => r.FindAll()).Returns(news.Object).Verifiable();

      // Act
      ActionResult actual = newsController.Index();

      // Assert
      mockNewsRepository.Verify();
      actual
         .AssertViewRendered()
         .ForView("Index")
         .WithViewData<News[]>()
         .ShouldBe(news);
   }
}

My view:

public ActionResult Index()
{
   FakeNewsRepository fakeNewsRepository = new FakeNewsRepository();
   IEnumerable<News> news = fakeNewsRepository.FindAll();
   return View(news);
}

I need some pointers on the way that I did it. Am I in the correct direction? What should I add, what should I leave out? I want to do unit testing, am I mixing it with integration testing? Any extra advice would be appreciated.

When I run this test in the NUnit GUI console then I get an error back and I'mnot sure what it means:

MyProject.Web.Tests.Controllers.NewsControllerTest.NewsController_Index:
Moq.MockVerificationException : The following setups were not matched:
INewsRepository r => r.FindAll()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自控 2024-10-13 15:19:43
public ActionResult Index()
{
    FakeNewsRepository fakeNewsRepository = new FakeNewsRepository();
    IEnumerable<News> news = fakeNewsRepository.FindAll();
    return View(news);
}

您不能在操作中模拟像这样硬编码的存储库。您在操作中实例化它,您将永远无法对此进行单元测试并模拟存储库。存储库需要作为依赖项注入。您可以使用接口并将该接口传递给控制器​​的构造函数:

public class HomeController: Controller
{
    private readonly IRepository _repository;
    public class HomeController(IRepository repository)
    {
        _repository = repository;
    }

    public ActionResult Index()
    {
        IEnumerable<News> news = _repository.FindAll();
        return View(news);
    }
}

现在,在单元测试中,您可以将存储库的模拟实例传递给控制器​​构造函数并定义期望。

另请注意,MVCContrib.TestHelper 旨在与 Rhino Mocks 配合使用。我不太确定它是否适用于起订量。

public ActionResult Index()
{
    FakeNewsRepository fakeNewsRepository = new FakeNewsRepository();
    IEnumerable<News> news = fakeNewsRepository.FindAll();
    return View(news);
}

You cannot mock the repository that is hardcoded like this in your action. You are instantiating it inside the action, you will never be able to unit test this and mock the repository. The repository needs to be injected as a dependency. You could use an interface and pass this interface to the constructor of your controller:

public class HomeController: Controller
{
    private readonly IRepository _repository;
    public class HomeController(IRepository repository)
    {
        _repository = repository;
    }

    public ActionResult Index()
    {
        IEnumerable<News> news = _repository.FindAll();
        return View(news);
    }
}

Now in your unit test you could pass the mocked instance of your repository to the controller constructor and define expectations.

Also notice that MVCContrib.TestHelper is designed to work with Rhino Mocks. I am not quite sure whether it works fine with Moq.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文