ASP.NET MVC 2:在视图和控制器中测试什么

发布于 2024-10-06 06:20:44 字数 1063 浏览 5 评论 0原文

我正在使用 MVC 2 与 MVC contrib 和 Rhino 模拟。

我需要帮助。这是一个痛苦的周末。我一直在尝试寻找有关如何使用 MVC contrib 对控制器进行测试的好文章。没有人能给我任何像样的答案。我想知道我需要在视图和控制器中测试什么?这是单元测试还是集成测试?

让我描述一下我的基本场景:我有一个索引视图,上面有一个网格。在这个网格中我有一个新闻项目列表。在此网格中,每行的最后一列中都有一个编辑链接,可将用户带到 EditNews 视图以编辑选定的新闻项目。此“索引”视图上还有一个“添加新闻”按钮,可将用户带到“创建新闻”视图。

现在考虑到当前的情况,我需要编写哪些测试?你们能否也指出它们是单元测试还是集成测试。我需要为编辑链接编写测试吗?我需要为“添加新闻”按钮编写测试吗?我是否需要编写一个测试来检查用户是否被允许访问此索引页面?这就是我目前拥有的:

[Test]
public void Index_Should_Redirect_To_Error_View_When_User_Does_Not_Have_Permission()
{
}

[Test]
public void Index_Should_Log_Exception_When_User_Does_Not_Have_Permission()
{
}

[Test]
public void Index_Should_Return_Default_View()
{
   // Act
   var result = newsController.Index();

   // Assert
   result
      .AssertViewRendered()
      .ForView("Index");
}

更新: 这是我的索引操作方法:

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

任何像样的文章或示例代码将不胜感激。另外,在 MVC contrib 上,代码示例没有多大用处。我想听到尽可能多的意见。

谢谢。

I'm using MVC 2 with MVC contrib and Rhino mocks.

I am in need of help. It has been a painful weekend. I have been trying to look for good articles on how to do testing on controllers using MVC contrib. And no one has any decent answers for me. I want to know what do I need to test on a view and in controllers? Is this unit testing or integration testing?

Let me describe my basic scenario: I have an Index view that has a grid on it. In this grid I have a list of news items. In this grid, in the last column of every row, is an Edit link that takes the user to the EditNews view to edit the selected news item. Also on this Index view is an Add News button that takes the user to the CreateNews view.

Now given the current scenario, what tests do I need to write? Could you guys please also indicate if they are unit tests or integration tests. Do I need to write a test for the Edit link? Do I need to write a test for the Add News button? Do I need to write a test to check if the user is allowed on this Index page? This is what I currently have:

[Test]
public void Index_Should_Redirect_To_Error_View_When_User_Does_Not_Have_Permission()
{
}

[Test]
public void Index_Should_Log_Exception_When_User_Does_Not_Have_Permission()
{
}

[Test]
public void Index_Should_Return_Default_View()
{
   // Act
   var result = newsController.Index();

   // Assert
   result
      .AssertViewRendered()
      .ForView("Index");
}

UPDATED:
Here is my Index action method:

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

Any decent articles or sample code would be appreciated. Also on MVC contrib, the code samples aren't of much use. I would like to hear as much opinions as possible.

Thanks.

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

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

发布评论

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

评论(1

那一片橙海, 2024-10-13 06:20:44

您需要区分单元测试、集成测试和 Web 测试。单元测试用于测试代码的不同组件,例如单独的控制器操作。集成测试用于测试代码与外部组件(例如从数据库读取和写入数据的存储库)之间的集成,而 Web 测试用于测试应用程序的整个用户场景,例如用户在地址栏中键入某个地址他的浏览器,单击某个按钮,...

MVCContrib.TestHelper 和 Rhino Mocks 旨在简化您的单元测试。因此,在您的场景中,您应该测试您的 Index 控制器操作。如果不显示该控制器操作包含的代码,则很难说出您到底需要测试什么。一般来说,您可以在控制器操作中进行单元测试,它验证输入,调用存储库上的正确方法,并根据结果返回正确的视图。

因此,假设您愿意测试以下 Index 操作:

public class HomeController: Controller
{
    private readony INewRepository _repository;
    public HomeController(INewRepository repository)
    {
        _repository = repository;
    }

    public ActionResult Index()
    {
        var news = _repository.GetNews();
        return View(news);
    }
}

以及相应的单元测试:

[TestClass]
public class HomeControllerTests : TestControllerBuilder
{
    private HomeController _sut;
    private INewsRepository _repositoryStub;

    [TestInitialize()]
    public void MyTestInitialize() 
    {
        _repositoryStub = MockRepository.GenerateStub<INewsRepository>();
        _sut = new UsersController(_repositoryStub);
        InitializeController(_sut);
    }

    [TestMethod]
    public void UsersController_Index_Should_Fetch_News_From_Repository()
    {
        // arrange
        var news = new News[0];
        _repositoryStub.Stub(x => x.GetNews()).Return(news);

        // act
        var actual = _sut.Index();

        // assert
        actual
            .AssertViewRendered()
            .WithViewData<News[]>()
            .ShouldBe(news);
    }
}

就您的视图而言,您需要编写 Web 测试来验证它们的行为是否符合预期。有不同的工具可能会帮助您,例如 Selenium 或直接使用 Web 测试(如果您拥有 Visual Studio 旗舰版)。 Steven Sanderson 还提出了一种有趣的方法 为了测试视图,您可以看一下。

You need to make the distinction between unit tests, integration tests and web tests. Unit tests are for testing different components of your code such as controller actions in isolation. Integration tests are for testing the integration between your code and external components such as a repository which reads and writes data from a database and a web test is for testing an entire user scenario of your application such as the user typing some address in the address bar of his browser, clicking on some button, ...

MVCContrib.TestHelper and Rhino Mocks are intended to simplify your unit tests. So in your scenario you should test your Index controller action. Without showing the code this controller action contains it is difficult to say what exactly do you need to test. In general what you could unit test in a controller action is that it validates the input, calls the proper methods on the repository and according to the results it returns the proper view.

So let's suppose that you have the following Index action you are willing to test:

public class HomeController: Controller
{
    private readony INewRepository _repository;
    public HomeController(INewRepository repository)
    {
        _repository = repository;
    }

    public ActionResult Index()
    {
        var news = _repository.GetNews();
        return View(news);
    }
}

And the corresponding unit test:

[TestClass]
public class HomeControllerTests : TestControllerBuilder
{
    private HomeController _sut;
    private INewsRepository _repositoryStub;

    [TestInitialize()]
    public void MyTestInitialize() 
    {
        _repositoryStub = MockRepository.GenerateStub<INewsRepository>();
        _sut = new UsersController(_repositoryStub);
        InitializeController(_sut);
    }

    [TestMethod]
    public void UsersController_Index_Should_Fetch_News_From_Repository()
    {
        // arrange
        var news = new News[0];
        _repositoryStub.Stub(x => x.GetNews()).Return(news);

        // act
        var actual = _sut.Index();

        // assert
        actual
            .AssertViewRendered()
            .WithViewData<News[]>()
            .ShouldBe(news);
    }
}

As far as your views are concerned you will need to write web tests to verify that they behave as expected. There are different tools that might help you here such as Selenium or directly use Web Tests if you have the Ultimate version of Visual Studio. Steven Sanderson also proposed an interesting approach for testing views you might take a look at.

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