问题
I'm trying to write a Unit Test with Moq to verify that a Registration was successful. My Test is as follows:
[TestMethod()]
public void RegisterTest()
{
//Arrange
var MockRepo = new Mock<IDataRepo>() ;
RegisterModel model = new RegisterModel
{
ConfirmPassword = "SamePassword",
Email = "myemail@address.com",
FirstName = "MyFirstName",
LastName = "MyLastName",
MiddleName = "MyMiddleName",
Password = "SamePassword"
};
MockRepo.Setup(ctx => ctx.Add(model)).Verifiable("Nothing was added to the Database");
//Act
AccountController target = new AccountController(MockRepo.Object);
//Assert
ActionResult actual = target.Register(model);
MockRepo.Verify(ctx => ctx.Add(It.IsAny<RegisterModel>()));
Assert.IsInstanceOfType(actual, typeof(ViewResult));
}
But it fails with the following Error
Expected invocation on the mock at least once, but was never performed: ctx => ctx.Add(It.IsAny())
However, When I debugged the Test Method, I noticed that the Add(T) method was actually called. The MOQ dll version is v4.0
UPDATE Account Controller:
public class AccountController : Controller
{
private IDataRepo _repo;
public AccountController(IDataRepo Repo)
{
_repo = Repo;
}
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
User user = _repo.Users.Where(u => u.Email == model.Email).FirstOrDefault();
if (user == null)
{
_repo.Add(new User
{
Email = model.Email,
Password = model.Password,
FirstName = model.FirstName,
LastName = model.LastName,
MiddleName = model.MiddleName
});
return View("RegistrationSuccess");
}
else
{
ModelState.AddModelError("UserExists", "This Email already Exists");
}
}
return View(model);
}
}
回答1:
Your problem is that your Mock expects a RegisterModel
instance
RegisterModel model = new RegisterModel
{
ConfirmPassword = "SamePassword",
Email = "myemail@address.com",
FirstName = "MyFirstName",
LastName = "MyLastName",
MiddleName = "MyMiddleName",
Password = "SamePassword"
};
MockRepo.Setup(ctx => ctx.Add(model))
but the Add
method gets called with an instance of the User
class
_repo.Add(new User
{
Email = model.Email,
Password = model.Password,
FirstName = model.FirstName,
LastName = model.LastName,
MiddleName = model.MiddleName
});
So, one way to get around this is to setup the mock to accept a User
instance.
RegisterModel model = new RegisterModel
{
ConfirmPassword = "SamePassword",
Email = "myemail@address.com",
FirstName = "MyFirstName",
LastName = "MyLastName",
MiddleName = "MyMiddleName",
Password = "SamePassword"
};
User expected = new User
{
Email = model.Email,
Password = model.Password,
FirstName = model.FirstName,
LastName = model.LastName,
MiddleName = model.MiddleName
};
MockRepo.Setup(ctx => ctx.Add(expected))
回答2:
I discovered an easier way to do it. Rather than generate your own User object you could call It.IsAny<User>()
and the test would run just fine. so my unit test now becomes..
//Arrange
var MockRepo = new Mock<IDataRepo>() ;
var MockMembership = new Mock<IMembership>();
RegisterModel model = new RegisterModel
{
ConfirmPassword = "SamePassword",
Email = "myemail@address.com",
FirstName = "MyFirstName",
LastName = "MyLastName",
MiddleName = "MyMiddleName",
Password = "SamePassword"
};
MockRepo.Setup(ctx => ctx.Add(It.IsAny<User>())).Verifiable("Nothing was added to the Database");
//Act
AccountController target = new AccountController(MockRepo.Object, MockMembership.Object);
//Assert
ActionResult actual = target.Register(model);
MockRepo.Verify(ctx => ctx.Add(It.IsAny<User>()));
Assert.IsInstanceOfType(actual, typeof(ViewResult));
来源:https://stackoverflow.com/questions/11790129/testing-specific-method-calls-with-parameters-using-moq