MVC 3:如何模拟 WebViewPage的实例?
我目前正在使用 Rhino Mocks 模拟框架。如何模拟 WebViewPageBase 其模型与 Rhino Mocks 或任何其他模拟框架?当我运行以下单元测试时,尝试访问 WebViewPageBase.Model 属性时会引发异常。
var repo = new Rhino.Mocks.MockRepository();
System.Web.Mvc.WebViewPage<IEnumerable<Tuple<string>>> page = repo.DynamicMock<System.Web.Mvc.WebViewPage<IEnumerable<Tuple<string>>>>();
Assert.IsNotNull(page.Model);//exception thrown, not a fail on Assert
使用不同的模拟框架会更容易实现这一点吗?
I'm currently using the Rhino Mocks mocking framework. How do you mock an instance of WebViewPageBase AND its Model with Rhino Mocks or any other mocking framework? When I run the following unit test, an exception is thrown when attempting to access the WebViewPageBase.Model property.
var repo = new Rhino.Mocks.MockRepository();
System.Web.Mvc.WebViewPage<IEnumerable<Tuple<string>>> page = repo.DynamicMock<System.Web.Mvc.WebViewPage<IEnumerable<Tuple<string>>>>();
Assert.IsNotNull(page.Model);//exception thrown, not a fail on Assert
Would this be more easily accomplished with a different mocking framework?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Scott Hanselman 在此处写了一篇关于使用 Rhino 模拟 MVC 3 类的精彩文章:
http://www. hanselman.com/blog/ASPNETMVCSessionAtMix08TDDAndMvcMockHelpers.aspx
Scott Hanselman has a great write-up on mocking MVC 3 classes with Rhino here:
http://www.hanselman.com/blog/ASPNETMVCSessionAtMix08TDDAndMvcMockHelpers.aspx
您确定要以这种方式获取视图页面吗,因为它没有模型等,因为它尚未全部设置。
更常见的是,您可以测试控制器并检查其返回值,如果您想检查的话,该返回值将包含渲染的 html。
假设您确实想对此进行单元测试,那么我看不出您的代码有任何本质上的错误。你可以尝试 Moq 而不是我使用的 Rhino。
这个问题也可能与您的问题相关,而没有Rhino如何处理抽象类:
模拟抽象Rhino 的类默认行为
Are you sure you want to get the view page this way as it won't have a model etc because it has not all been setup.
More typically you would test the controller and inspects its return value which would contain the rendered html if you want to check that.
Assuming you really do want to unit test this, then I don't see anything intrinsically wrong with your code. You could try Moq instead of Rhino which is what I use.
This question may also be pertinent to your issue without how Rhino handles abstract classes:
Mock abstract class default behaviour with Rhino