Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8061209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:15:40+00:00 2026-06-05T10:15:40+00:00

I am trying to write some unit tests for my ApiController and faced some

  • 0

I am trying to write some unit tests for my ApiController and faced some issues. There is a nice extension method called Request.CreateResponse that helps a lot with generating response.

public HttpResponseMessage Post(Product product)
{
  var createdProduct = repo.Add(product);
  return this.Request.CreateResponse(HttpStatusCode.Created, createdProduct);
}

Is there any way to mock CreateResponse without using of partial mocks or direct using of “new HttpResponseMessage(…)”?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:15:42+00:00Added an answer on June 5, 2026 at 10:15 am

    Another way to solve this is to do the following:

    controller.Request = new HttpRequestMessage();
    controller.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, 
                                      new HttpConfiguration());
    

    If you are upgrading to webapi 5.0, then you’ll need to change this to:

    controller.Request = new HttpRequestMessage();
    controller.Request.SetConfiguration(new HttpConfiguration());
    

    The reason why you need to do this is because you have to have Request populated on the controller otherwise the extension methods on Request won’t work. You also have to have an HttpConfiguration set on the Request otherwise routing and other parts of the pipeline won’t function correctly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write some Unit Tests to test some custom NSOperations that
I am trying to use python's unittest library to write some unit tests. I
I am trying to write some unit tests for a small web service written
I'm trying to write some unit tests for a JPA model that I've built
I am trying to write some unit tests for my MVC3 project (the first
I'm trying to write some unit tests for Command object validations. When my command
I'm trying to write some unit tests for a gwt-dispatch service with JUnit. I'm
I'm trying to find a way to write some unit-tests that can be used
I am trying to write some unit tests that test the endpoints for my
I'm working with Subsonic's SimpleRepository, and I'm trying to write some unit tests so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.