最佳答案
I would like to set up a method with Moq twice but it seems that the last one overrides the previous ones. Here's my initial setup:
string username = "foo";
string password = "bar";
var principal = new GenericPrincipal(
new GenericIdentity(username),
new[] { "Admin" });
var membershipServiceMock = new Mock<IMembershipService>();
membershipServiceMock.Setup(ms =>
ms.ValidateUser(username, password)
).Returns(new ValidUserContext {
Principal = principal
});
This works out fine but I want this to return new ValidUserContext()
if the username or password is different to the username
and password
variables as above. To do that, I added another setup but this time it overrides the above one and always applies it:
membershipServiceMock.Setup(ms =>
ms.ValidateUser(It.IsAny<string>(), It.IsAny<string>())
).Returns(
new ValidUserContext()
);
What is the most elegant way of handling this type of situation with Moq?
Edit
I solved the problem with the below approach but I guess there is a better way of handling this:
var membershipServiceMock = new Mock<IMembershipService>();
membershipServiceMock.Setup(ms =>
ms.ValidateUser(It.IsAny<string>(), It.IsAny<string>())
).Returns<string, string>((u, p) =>
(u == username && p == password) ?
new ValidUserContext {
Principal = principal
}
: new ValidUserContext()
);