使用 Rhino Mocks 模拟 ASP.NET MVC2 中的控制器操作
我无法理解如何有效且高效地为一个简单的控制器操作构建模拟单元测试,该操作创建视图模型的实例并将其传递给视图。
public ActionResult Index()
{
IndexViewModel viewModel = new IndexViewModel();
return View(viewModel);
}
有人可以告诉我如何为控制器操作编写单元测试,以确保该操作生成视图模型类的实例,并将其分配为视图的模型。
当然,我理解 TDD 说我应该先编写测试,然后构建上面的内容,但我在掌握基础知识时遇到了困难。对您传递的任何代码的解释也很棒。谢谢
I am having trouble understanding how I can effectively and efficiently building a mocking unit test for a simple controller action that creates an instance of a viewmodel and passes it to a view.
public ActionResult Index()
{
IndexViewModel viewModel = new IndexViewModel();
return View(viewModel);
}
Can someone please give me an idea how I would write a unit test for a controller action that would ensure that the action generates an instance of a viewmodel class, and assigns it as the model for the view.
I understand, of course, that TDD says I should write the test first, and then build the above, but I having trouble grasping the fundamentals. An explanation of any code you pass on would be great, too. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这只是一个简短的示例,说明您可以执行以下操作来测试此内容:
因此,您在控制器上调用 Index 方法,访问 viewModel 并确保它的类型为 IndexViewModel,然后断言它不为 null。
希望这有帮助。
This is just a brief example of what you could do to test this:
So you are calling the Index method on the controller, accessing the viewModel and making sure it is of type IndexViewModel and then you assert that it is not null.
Hope this helps.