I'm getting error like below:
An exception of type 'System.NullReferenceException' occurred in magazyn.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.
This error is thrown in this method:
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Storage storage = unitOfWork.storageRepository.GetByID(id.Value);
if (storage == null)
{
return HttpNotFound();
}
return PartialView(storage);
}
in this line:
Storage storage = unitOfWork.storageRepository.GetByID(id.Value);
Here my unit Test code:
[TestMethod]
public void Details()
{
int? a = null;
var result = SC.Details(a);
Assert.IsInstanceOfType(result, typeof(HttpStatusCodeResult));
var code = result as HttpStatusCodeResult;
Assert.AreEqual((int)HttpStatusCode.BadRequest, code.StatusCode);
a = 1;
result = SC.Details(a);
Assert.IsInstanceOfType(result, typeof(HttpNotFoundResult));
}
I tried debugging this test and all values are passed correctly. In db there is a record with Id=1
What is wrong with this code?
@Update with Test initialization:
[TestInitialize]
public void Initialize()
{
Mock<IUnitOfWork> mock = new Mock<IUnitOfWork>();
fakeRepo = mock.Object;
SC = new StorageController(fakeRepo);
}