起订量异常:验证采用参数的方法调用

发布于 2024-09-06 23:42:23 字数 1701 浏览 4 评论 0原文

我想测试控制器中 _eventManager 上的“Create”方法是否被调用。当我运行测试时,出现以下异常:

测试方法 Baigent.TheDoNation.Application.Tests.EventControllerTest.Create_Post_IfModelIsValidRedirectToSuccessivelyCreatedViewOccurs 抛出异常:System.ArgumentException:不可重写成员上的无效设置: m => m.CreateEvent(It.IsAny(), It.IsAny())。

控制器的代码是:

    public ActionResult Create(Event eventObject, FormCollection collection)
    {
        if (ModelState.IsValid)
        {
            _eventManager.CreateEvent(eventObject, User.Identity.Name);

            return RedirectToAction("SuccessfullyCreated", new { });
        }

        // Invalid - redisplay form with errors
        return View(GetEventViewModel(eventObject));
    }

_eventManager 字段在构造函数中设置。我的测试是:

        var eventManagerMock = new Mock<EventManager>(new FakeEventsRepository());
        eventManagerMock.Setup(m => m.CreateEvent(It.IsAny<Event>(), It.IsAny<String>())).Verifiable("No call to CreateEvent on the EventManager was made");

        var eventController = new EventController(eventManagerMock.Object);

        var newEvent = new Event {Name = "Test Event", Date = DateTime.Now, Description = "Test description"};

        // Act
        var result = eventController.Create(newEvent, new FormCollection()) as RedirectToRouteResult;

        // Assert
        eventManagerMock.Verify(m => m.CreateEvent(It.IsAny<Event>(), It.IsAny<String>())); 

        Assert.IsNotNull(result, "RedirectToRouteResult should be returned");
        Assert.AreEqual("SuccessfullyCreated", result.RouteValues["action"], "Redirect should be to SuccessfullyCreated view");

请帮忙!

I want to test that the "Create" method on the _eventManager in my controller gets called. When I run my test, I get the following exception:

Test method Baigent.TheDoNation.Application.Tests.EventControllerTest.Create_Post_IfModelIsValidRedirectToSuccessfullyCreatedViewOccurs threw exception: System.ArgumentException: Invalid setup on a non-overridable member:
m => m.CreateEvent(It.IsAny(), It.IsAny()).

The code for the controller is:

    public ActionResult Create(Event eventObject, FormCollection collection)
    {
        if (ModelState.IsValid)
        {
            _eventManager.CreateEvent(eventObject, User.Identity.Name);

            return RedirectToAction("SuccessfullyCreated", new { });
        }

        // Invalid - redisplay form with errors
        return View(GetEventViewModel(eventObject));
    }

The _eventManager field gets set in the constructor. My test is :

        var eventManagerMock = new Mock<EventManager>(new FakeEventsRepository());
        eventManagerMock.Setup(m => m.CreateEvent(It.IsAny<Event>(), It.IsAny<String>())).Verifiable("No call to CreateEvent on the EventManager was made");

        var eventController = new EventController(eventManagerMock.Object);

        var newEvent = new Event {Name = "Test Event", Date = DateTime.Now, Description = "Test description"};

        // Act
        var result = eventController.Create(newEvent, new FormCollection()) as RedirectToRouteResult;

        // Assert
        eventManagerMock.Verify(m => m.CreateEvent(It.IsAny<Event>(), It.IsAny<String>())); 

        Assert.IsNotNull(result, "RedirectToRouteResult should be returned");
        Assert.AreEqual("SuccessfullyCreated", result.RouteValues["action"], "Redirect should be to SuccessfullyCreated view");

Please help!

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

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

发布评论

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

评论(3

夏日浅笑〃 2024-09-13 23:42:23

该异常告诉您您正在尝试覆盖非虚拟成员,这是不可能的。

Moq(以及Rhino Mocks和NMock)只能覆盖虚拟成员(包括纯接口成员)。

请参阅这里有更详细的解释

The exception tells you that you are trying to override a non-virtual member, which is impossible.

Moq (as well as Rhino Mocks and NMock) can only override virtual members (including pure interface members).

See here for a more detailed explanation.

彩虹直至黑白 2024-09-13 23:42:23

Moq 只能模拟 EventManager 类型的虚拟成员。您应该考虑提取 IEventManager 接口,或将 CreateEvent 方法设为虚拟。

Moq can only mock virtual members of your EventManager type. You should consider extracting a IEventManager interface, or make the CreateEvent method virtual.

沉睡月亮 2024-09-13 23:42:23

您要么必须将该方法设为虚拟,要么需要定义一个具有 CreateEvent() 方法的接口,然后模拟该接口:]

您现在想要模拟一个方法,其中 Moq 没有直接权限来覆盖它。

You will either have to make the method Virtual or you will need to define an interface that has the method CreateEvent() and then Mock the interface :]

You now want to mock a Method in which Moq has no direct rights to override it.

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