Reputation:
I have the method
public async Task<ActionResult> CreateRole(RoleModel roleViewModel)
{
if (ModelState.IsValid)
{
RoleModel role = new RoleModel(roleViewModel.Name);
// Save the new Description property:
role.Description = roleViewModel.Description; // <--- Here you have assign the Description value
IdentityResult roleResult = await roleManager.CreateRoleAsync(role);
if (roleResult.Succeeded)
{
return RedirectToAction("Index");
}
else
{
AddErrorsFromResult(roleResult);
}
}
return View(roleViewModel);
}
which create the role, it work right, but when i want to make the mock test I have the null IdentityResult. I can't understand what the problem.
my test
public async Task CanCreateRoleAsync()
{
//arrange
Task<IdentityResult> successResult = Task.FromResult(IdentityResult.Success);
Mock<IRoleManagerRepository> mockRole = new Mock<IRoleManagerRepository>();
Mock<IUserManagerRepository> mockUser = new Mock<IUserManagerRepository>();
RoleController controller = new RoleController(mockRole.Object, mockUser.Object);
RoleModel model = new RoleModel { Id = "test-test-test-test", Name = "test", Description = "test user" };
mockRole.Setup(m => m.CreateRoleAsync(model)).Returns(successResult);
//action
ActionResult result = await controller.CreateRole(model);
//assert
Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
}
and the method which create the role
public async Task<IdentityResult> CreateRoleAsync(RoleModel applicationRole)
{
IdentityResult result = await userRole.CreateAsync(applicationRole);
return result;
}
null in this point
and error called NullReferenceException object reference does not indicate an object instance.
Upvotes: 1
Views: 370
Reputation: 56716
Guess it is because the input does not match. Your setup uses one RoleModel instance, and then the actual method call uses a different one created inside CreateRole
.
Set it up so it can take any object:
mockRole.Setup(m => m.CreateRoleAsync(It.IsAny<RoleModel>()))
.Returns(successResult);
Upvotes: 1