Unit Testing error "Object reference not set to an instance of an object."
asp.net-mvc, asp.net-mvc-4, c#, rhino-mocks, unit-testing
Solution
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.
Problem
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?