0

I'm unit testing a simple post:

public HttpResponseMessage<Document> PostDocument(Document document) 
{
    document = repository.Add(document); 

    var response = new HttpResponseMessage<Document>(document, HttpStatusCode.Created); 

    var uri = Url.Route(null, new { id = document.Id }); 

    response.Headers.Location = new Uri(Request.RequestUri, uri); 

    return response; 
}

However, the 'URL' and 'Request' are obviously going to be null.

Is there an alternative to mocking out ControllerContext and HttpContext?

Update:

Changed it to:

 public HttpResponseMessage<Document> PostDocument(Document document,Uri location = null) 
{
    document = repository.Add(document); 

    var response = new HttpResponseMessage<Document>(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; 
}

Update 2:

This is better:

public HttpResponseMessage<Document> 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<Document> PostDocument(Document document, Uri location) 
{
    document = repository.Add(document); 

    var response = new HttpResponseMessage<Document>(document, HttpStatusCode.Created);
    response.Headers.Location = location;
    return response; 
}
1

3 Answers 3

1

The Request property should be settable, so you only have to set the ControllerContext (which should have a no-arg constructor so you shouldn't even have to mock).

Sign up to request clarification or add additional context in comments.

Comments

1

Using FakeItEasy I got it to work doing this in the TestInitialize.

this.Controller.ControllerContext = new System.Web.Http.Controllers.HttpControllerContext();
this.Controller.Request = A.Fake<HttpRequestMessage>();

Comments

0

Your method might recieve HttpRequestMessage as parameter.

 public HttpResponseMessage<Document> PostDocument(Document document, HttpRequestMessage message)
{

} 

You can take RequestUri from it. In your unit tests you can put test double of HttpRequestMessage object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.