The value cannot be null. Parameter Name: Query

I am creating a unit test using nunit and all this code works fine at runtime.

I have this protected HttpResponseMessage code below, which is called by my controller when it returns.

However, the error:

"The value cannot be empty. The parameter name is displayed: query."

And when I check the request, it is actually null .

Question: How do I encode my unit test to return an HttpResponseMessage ?

This line displays an error:

  protected HttpResponseMessage Created<T>(T result) => Request.CreateResponse(HttpStatusCode.Created, Envelope.Ok(result)); 

Here is my controller:

  [Route("employees")] [HttpPost] public HttpResponseMessage CreateEmployee([FromBody] CreateEmployeeModel model) { //**Some code here**// return Created(new EmployeeModel { EmployeeId = employee.Id, CustomerId = employee.CustomerId, UserId = employee.UserId, FirstName = employee.User.FirstName, LastName = employee.User.LastName, Email = employee.User.Email, MobileNumber = employee.MobileNumber, IsPrimaryContact = employee.IsPrimaryContact, OnlineRoleId = RoleManager.GetOnlineRole(employee.CustomerId, employee.UserId).Id, HasMultipleCompanies = EmployeeManager.HasMultipleCompanies(employee.UserId) }); } 
+12
source share
3 answers

The reason you get:

An exception of type "System.ArgumentNullException" occurred in System.Web.Http.dll, but was not processed in the user code Additional information: The value cannot be null.

is that the Request object is null .

enter image description here

The solution for this is to instantiate your controller in your tests, for example:

  var myApiController = new MyApiController { Request = new System.Net.Http.HttpRequestMessage(), Configuration = new HttpConfiguration() }; 

Thus, when creating a new instance of the MyApiController class MyApiController we initialize the Request object. In addition, you must also provide a related configuration object.

Finally, an example of Unit Test for your Api Controller might be:

 [TestClass] public class MyApiControllerTests { [TestMethod] public void CreateEmployee_Returns_HttpStatusCode_Created() { // Arrange var controller = new MyApiController { Request = new System.Net.Http.HttpRequestMessage(), Configuration = new HttpConfiguration() }; var employee = new CreateEmployeeModel { Id = 1 }; // Act var response = controller.CreateEmployee(employee); // Assert Assert.AreEqual(response.StatusCode, HttpStatusCode.Created); } } 
+13
source

I think what happens is that you do not instantiate or assign the Request property ( HttpRequestMessage ) when updating your controller. I consider it mandatory to specify the request before calling the Api method through your unit test.

You may also need a configuration ( HttpConfiguration ):

 sut = new YourController() { Request = new HttpRequestMessage { RequestUri = new Uri("http://www.unittests.com") }, Configuration = new HttpConfiguration() }; 

Let me know if this works.

+3
source

In addition, if your controller has injections, you can do:

 var controller= new MyController(injectionA, injectionB, injectionC) { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; 

I find them all in an easy-to-understand white paper now.

0
source

All Articles