gpt4 book ai didi

asp.net-mvc - 最小起订量异常 : Verifying a method call that takes parameters

转载 作者:行者123 更新时间:2023-11-28 20:12:48 24 4
gpt4 key购买 nike

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

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

Controller 的代码是:

    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");

请帮忙!

最佳答案

异常告诉您您正试图覆盖一个非虚拟成员,这是不可能的。

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

参见 here for a more detailed explanation .

关于asp.net-mvc - 最小起订量异常 : Verifying a method call that takes parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3116882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com