Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit Testing ASP.NET Web API
    primarykey
    data
    text
    <p>I'm unit testing a simple post:</p> <pre><code>public HttpResponseMessage&lt;Document&gt; PostDocument(Document document) { document = repository.Add(document); var response = new HttpResponseMessage&lt;Document&gt;(document, HttpStatusCode.Created); var uri = Url.Route(null, new { id = document.Id }); response.Headers.Location = new Uri(Request.RequestUri, uri); return response; } </code></pre> <p>However, the 'URL' and 'Request' are obviously going to be null. </p> <p>Is there an alternative to mocking out ControllerContext and HttpContext?</p> <p>Update:</p> <p>Changed it to:</p> <pre><code> public HttpResponseMessage&lt;Document&gt; PostDocument(Document document,Uri location = null) { document = repository.Add(document); var response = new HttpResponseMessage&lt;Document&gt;(document, HttpStatusCode.Created); if (location == null) { var uri = Url.Route(null, new { id = document.Id }); location = new Uri(Request.RequestUri, uri); } response.Headers.Location = location; return response; } </code></pre> <p>Update 2:</p> <p>This is better:</p> <pre><code>public HttpResponseMessage&lt;Document&gt; PostDocument(Document document) { var uri = Url.Route(null, new { id = document.Id }); var location = new Uri(Request.RequestUri, uri); return PostDocument(document, location); } [NonAction] public HttpResponseMessage&lt;Document&gt; PostDocument(Document document, Uri location) { document = repository.Add(document); var response = new HttpResponseMessage&lt;Document&gt;(document, HttpStatusCode.Created); response.Headers.Location = location; return response; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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