Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Moq to test methods that accept non-primative arguments
    text
    copied!<p>I'm trying to write a test for an ASP.Net MVC controller action.</p> <p>I'd like to test that the action invokes a particular method on an injected service, so I'm mocking the service and using .Verify.</p> <p>So in the simple case, I have the following action in my controller:</p> <pre><code> [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(string title) { _cmsService.AddPage(title); return View("Edit"); } </code></pre> <p>using the service interface...</p> <pre><code>public interface ICmsService { void AddPage(string request); } </code></pre> <p>and the following test...</p> <pre><code> //Arrange const string pageTitle = "Test Page"; var cmsService = new Mock&lt;ICmsService&gt;(); cmsService.Setup(service =&gt; service.AddPage(pageTitle)); var controller = new PageController(cmsService.Object); //Act var result = controller.Create(pageTitle) as ViewResult; //Assert cmsService.Verify(service =&gt; service.AddPage(pageTitle), Times.Once()); </code></pre> <p>Now I want to refactor my service operation to use request and response objects...</p> <pre><code>public interface ICmsService { CmsServiceAddPageResponse AddPage(CmsServiceAddPageRequest request); } </code></pre> <p>So I change my action accordingly...</p> <pre><code> public ActionResult Create(string title) { var request = new CmsServiceAddPageRequest() { PageName = title }; var response = _cmsService.AddPage(request); return View("Edit"); } </code></pre> <p>and also my test...</p> <pre><code> //Arrange const string pageTitle = "Test Page"; var cmsService = new Mock&lt;ICmsService&gt;(); var request = new CmsServiceAddPageRequest() {PageName = pageTitle}; cmsService.Setup(service =&gt; service.AddPage(request)); var controller = new PageController(cmsService.Object); //Act var result = controller.Create(pageTitle) as ViewResult; //Assert cmsService.Verify(service =&gt; service.AddPage(request), Times.Once()); </code></pre> <p>But now when I run the test, I get the following message...</p> <pre><code>TestCase 'Web.Test.PageControllerTest.CreateNewPagePost' failed: Moq.MockException : Invocation was performed more than once on the mock: service =&gt; service.AddPage(value(Web.Test.PageControllerTest+&lt;&gt;c__DisplayClass1).request) at Moq.Mock.ThrowVerifyException(IProxyCall expected, Expression expression, Times times) at Moq.Mock.VerifyCalls(Interceptor targetInterceptor, MethodCall expected, Expression expression, Times times) at Moq.Mock.Verify[T,TResult](Mock mock, Expression`1 expression, Times times, String failMessage) at Moq.Mock`1.Verify[TResult](Expression`1 expression, Times times) PageControllerTest.cs(67,0): at Web.Test.PageControllerTest.CreateNewPagePost() </code></pre> <p>What should I be doing to test a method that accepts a non-primitive type?</p> <p>Thanks</p> <p>Sandy</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload