3
    public HttpResponseMessage Save(IList<Models.Activity> activities)
    {
       //some controller code
        return Request.CreateResponse(HttpStatusCode.Created, activities, Configuration);
    }

How do I unit test that activities is correctly passed to the Request.CreateResponse?

1 Answer 1

7

You have two options here.

You can either set up the right context so that CreateResponse works. This blog post has a demonstration of that with PostProductReturnsCreatedStatusCode:

Here is the link

Or you can submit a request to an in-memory server like I mention here:

MSDN Link

The first option is more of a unit test because it doesn't go through the Web API pipeline, but it's also harder to setup and configure.

If you need to test the response body as well, you can use code that looks like this to get the entity that's being sent back:

ObjectContent content = response.Content as ObjectContent;
IList<Models.Activity> responseActivities = content.Value;
Sign up to request clarification or add additional context in comments.

2 Comments

I came across the first link on googling, but it only verifies the status code. I need to verify the resource (in this case 'activities') too.
Updated the answer. Hope that answers your question.

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.