有人可以帮我用最小起订量模拟这个 ASP.NET MVC 控制器吗?

发布于 2024-09-06 16:07:56 字数 1740 浏览 4 评论 0原文

我正在尝试使用 Moq 模拟 ASP.NET MVC2 控制器,但出现错误,因为我正在尝试模拟不可覆盖的属性。请问我应该怎么做?

注意:我试图模拟的控制器是(abstractASP.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 技术交流群。

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

发布评论

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

评论(1

遗心遗梦遗幸福 2024-09-13 16:07:56

对于测试,我在测试类中创建一个测试类控制器,如下所示:

protected class TestController : Controller { }

然后我有一个可用于测试的独立测试类。然后你就可以使用这个 TestController 实例。

For tests, I create a test class controller inside the test class like:

protected class TestController : Controller { }

And then I have an isolated test class that I can use for the tests. Then you can use this TestController instance.

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