Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing error "Object reference not set to an instance of an object."

In my controller I want to test if the controller is calling the repository method. Here is the method in controller

[HttpGet]
public ActionResult GetModulePropertyName(string moduleTypeValue)
{
  var temp = _modulerepository.GetModuleKindPropertyNames(moduleTypeValue);

  IList<Property> model = temp
     .Select(item => new Property {Name = item})
     .ToList();

  return PartialView("GetModulePropertyName",model);
}

And here is the test method

[TestMethod]
public void GetModulePropertyName_Action_Calls_GetModuleKindPropertyNames()
{
  _mockRepository.Stub(x => x.GetModuleKindPropertyNames(Arg<string>.Is.Anything));

  _controller.GetModulePropertyName(Arg<string>.Is.Anything);

  _mockRepository.AssertWasCalled(x=>x.GetModuleKindPropertyNames(Arg<string>.Is.Anything));
}

It throws an error saying

Test method AdminPortal.Tests.ModuleControllerTests.GetModulePropertyName_Action_Calls_GetModuleKindPropertyNames threw exception: 
System.NullReferenceException: Object reference not set to an instance of an object.
    at System.Linq.Queryable.Select(IQueryable`1 source, Expression`1 selector)
   at AdminPortal.Areas.Hardware.Controllers.ModuleController.GetModulePropertyName(String moduleTypeValue) in ModuleController.cs: line 83
   at AdminPortal.Tests.ModuleControllerTests.GetModulePropertyName_Action_Calls_GetModuleKindPropertyNames() in ModuleControllerTests.cs: line 213

I'm using RhinoMock as mocking tool. Can someone help with what mistake i'm making?

like image 638
Cybercop Avatar asked Aug 16 '13 12:08

Cybercop


People also ask

How do I fix object reference not set to an instance of an object?

To fix "Object reference not set to an instance of an object," you should try running Microsoft Visual Studio as an administrator. You can also try resetting the user data associated with your account or updating Microsoft Visual Studio to the latest version.

How do I fix this error object reference not set to an instance of an object in unity?

Common solution:Check if your variable is attached to the object in question (you can find more clues in the other error lines). You might destroy the object before you use it. Check if it's the case. You might start the coroutine before you define the variable.

What causes object reference not set to an instance of an object?

new List<string>()” prevents the “Object reference not set to an instance of an object” exception. Some of the most common causes are settings, database calls, or API-type calls not returning expected values. For example, you add a new field to your database and don't populate default values for every record.

How do I fix NullReferenceException in C#?

Solutions to fix the NullReferenceException To prevent the NullReferenceException exception, check whether the reference type parameters are null or not before accessing them. In the above example, if(cities == null) checks whether the cities object is null or not.


2 Answers

After stubbing the method use Return to indicate what should it return, for example:

_mockRepository
  .Stub(x => x.GetModuleKindPropertyNames(Arg<string>.Is.Anything))
  .Return(Enumerable.Empty<string>().AsQueryable());

Also, change this line:

_controller.GetModulePropertyName(Arg<string>.Is.Anything);

to this:

_controller.GetModulePropertyName(string.Empty);

As the exception explains - Arg is only to be used in mock definitions.

like image 188
BartoszKP Avatar answered Sep 18 '22 02:09

BartoszKP


You don't have a return on your stub.

_mockRepository.Stub(x => x.GetModuleKindPropertyNames(Arg<string>.Is.Anything));

without that return, this line will be running a lambda against a null reference

 IList<Property> model = temp.Select(item => new Property {Name = item}).ToList();

so:

    _mockRepository.Stub(x => x.GetModuleKindPropertyNames(Arg<string>.Is.Anything)).Return(new Module[]{}); // set some return data here
like image 22
Slicksim Avatar answered Sep 18 '22 02:09

Slicksim