Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>this code should demonstrate the basics of a post test. Assumes you have a repository injected into the controller. I am using MVC 4 RC not Beta here if you are using Beta the Request.CreateResponse(... is a little different so give me a shout...</p> <p>Given controller code a little like this:</p> <pre><code>public class FooController : ApiController { private IRepository&lt;Foo&gt; _fooRepository; public FooController(IRepository&lt;Foo&gt; fooRepository) { _fooRepository = fooRepository; } public HttpResponseMessage Post(Foo value) { HttpResponseMessage response; Foo returnValue = _fooRepository.Save(value); response = Request.CreateResponse&lt;Foo&gt;(HttpStatusCode.Created, returnValue, this.Configuration); response.Headers.Location = "http://server.com/foos/1"; return response; } } </code></pre> <p>The unit test would look a little like this (NUnit and RhinoMock)</p> <pre><code> Foo dto = new Foo() { Id = -1, Name = "Hiya" }; IRepository&lt;Foo&gt; fooRepository = MockRepository.GenerateMock&lt;IRepository&lt;Foo&gt;&gt;(); fooRepository.Stub(x =&gt; x.Save(dto)).Return(new Foo() { Id = 1, Name = "Hiya" }); FooController controller = new FooController(fooRepository); controller.Request = new HttpRequestMessage(HttpMethod.Post, "http://server.com/foos"); //The line below was needed in WebApi RC as null config caused an issue after upgrade from Beta controller.Configuration = new System.Web.Http.HttpConfiguration(new System.Web.Http.HttpRouteCollection()); var result = controller.Post(dto); Assert.AreEqual(HttpStatusCode.Created, result.StatusCode, "Expecting a 201 Message"); var resultFoo = result.Content.ReadAsAsync&lt;Foo&gt;().Result; Assert.IsNotNull(resultFoo, "Response was empty!"); Assert.AreEqual(1, resultFoo.Id, "Foo id should be set"); </code></pre>
 

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