起订量异常:验证采用参数的方法调用
我想测试控制器中 _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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该异常告诉您您正在尝试覆盖非虚拟成员,这是不可能的。
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.
Moq 只能模拟
EventManager
类型的虚拟成员。您应该考虑提取IEventManager
接口,或将CreateEvent
方法设为虚拟。Moq can only mock virtual members of your
EventManager
type. You should consider extracting aIEventManager
interface, or make theCreateEvent
method virtual.您要么必须将该方法设为虚拟,要么需要定义一个具有 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.