有人可以帮我用最小起订量模拟这个 ASP.NET MVC 控制器吗?
我正在尝试使用 Moq 模拟 ASP.NET MVC2 控制器
,但出现错误,因为我正在尝试模拟不可覆盖的属性。请问我应该怎么做?
注意:我试图模拟的控制器是(abstract
)ASP.NET MVC2 Controller
...而不是自定义控制器。为什么?我正在尝试测试我所做的一些自定义控制器扩展。我实际上没有自定义控制器类。
代码:
// My own test helper magic shiz.
httpContextBaseMock = MockHelpers.GetAnHttpContextBaseMock();
controllerContextMock = new Mock<ControllerContext>();
controllerContextMock.Setup(x => x.HttpContext)
.Returns(httpContextBaseMock.Object);
controllerMock = new Mock<Controller>();
controllerMock.SetupGet(x => x.RouteData)
.Returns(RestMockHelpers.MockRouteData().Object);
最后一行失败并显示...
System.ArgumentException:无效 在不可重写的成员上设置:x => x.RouteData
所以我想,我不应该嘲笑 controllerContext
,而只是创建它的一个实例..就像 REA_ANDREW 在他的问题中做了 ...
var controllerContext = new ControllerContext(_httpContextBaseMock.Object,
new RouteData(), new Mock<ControllerBase>().Object);
var controller = new Controller(); <-- Cannot do this.
Controller class is abstract.
所以我不确定我是否需要制作自己的假控制器类,在一些测试辅助实用程序不执行任何操作,只是继承自 Controller
。然后实例化它。
但我觉得这一切都应该使用模拟来完成,而不是从一些开始,然后创建一些实例......
我很困惑:(
更新:
我被要求解释我正在尝试测试的代码。我我已经制作了一个自定义 ViewResult,并且构造函数设置了一个字符串属性,我只是确保设置了该属性。
// Act.
var myResult = new MyResult(controllerMock.Object);
// Assert.
Assert.NotNull(myResult);
Assert.AreEqual("controllerName", myResult.ControllerName);
I'm trying to mock an ASP.NET MVC2 Controller
using Moq but I get an error because i'm trying to mock an non-overridable property. How should I be doing this, please?
NOTE: the controller I'm trying to mock up is the (abstract
) ASP.NET MVC2 Controller
... not a custom controller. Why? I'm trying to test some custom controller extensions I have made. I don't actually have a custom controller class.
Code:
// My own test helper magic shiz.
httpContextBaseMock = MockHelpers.GetAnHttpContextBaseMock();
controllerContextMock = new Mock<ControllerContext>();
controllerContextMock.Setup(x => x.HttpContext)
.Returns(httpContextBaseMock.Object);
controllerMock = new Mock<Controller>();
controllerMock.SetupGet(x => x.RouteData)
.Returns(RestMockHelpers.MockRouteData().Object);
That last line fails with...
System.ArgumentException : Invalid
setup on a non-overridable member: x
=> x.RouteData
So then I thought, I shouldn't be mocking the controllerContext
, but just creating an instance of it.. like what REA_ANDREW did in his SO question ...
var controllerContext = new ControllerContext(_httpContextBaseMock.Object,
new RouteData(), new Mock<ControllerBase>().Object);
var controller = new Controller(); <-- Cannot do this.
Controller class is abstract.
So i'm not sure if I need to make my own fake controller class, in some test helper utility that does nothing, but just inherits from Controller
. Then instantiate that.
But I feel that it should be all done using mock's, instead of starting out with some, then making some instances...
I'm so confused :(
Update:
I was asked to explain what code i'm trying to test. I've got a custom ViewResult i've made and the constructor set a single string property. I'm just making sure that property is set.
// Act.
var myResult = new MyResult(controllerMock.Object);
// Assert.
Assert.NotNull(myResult);
Assert.AreEqual("controllerName", myResult.ControllerName);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于测试,我在测试类中创建一个测试类控制器,如下所示:
然后我有一个可用于测试的独立测试类。然后你就可以使用这个 TestController 实例。
For tests, I create a test class controller inside the test class like:
And then I have an isolated test class that I can use for the tests. Then you can use this TestController instance.