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

爷,独闯天下 提交于 2019-12-05 11:09:33

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!