单元测试错误“对象引用未设置为对象的实例。”

Cyb*_*cop 5 c# asp.net-mvc unit-testing rhino-mocks asp.net-mvc-4

在我的控制器中,我想测试控制器是否正在调用存储库方法。这是控制器中的方法

[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);
}
Run Code Online (Sandbox Code Playgroud)

这是测试方法

[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));
}
Run Code Online (Sandbox Code Playgroud)

它抛出一个错误

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
Run Code Online (Sandbox Code Playgroud)

我正在使用RhinoMock作为模拟工具。有人可以帮我解决什么错误吗?

Bar*_*zKP 5

在存根方法之后,使用Return来指示它应该返回什么,例如:

_mockRepository
  .Stub(x => x.GetModuleKindPropertyNames(Arg<string>.Is.Anything))
  .Return(Enumerable.Empty<string>().AsQueryable());
Run Code Online (Sandbox Code Playgroud)

另外,更改此行:

_controller.GetModulePropertyName(Arg<string>.Is.Anything);
Run Code Online (Sandbox Code Playgroud)

对此:

_controller.GetModulePropertyName(string.Empty);
Run Code Online (Sandbox Code Playgroud)

正如例外所解释的那样 -Arg仅用于模拟定义。