问题
Im using FakeItEasy as mocking framework in my unit tests. Method fakeUserService.AddUser is mocked to returned new MwbeUser object with some non-empty values inside method AddUser
A.CallTo(() => fakeUserService.AddUser(new MwbeUserRegistrationIn()
{
UserName = userName,
FirstName = firstName,
SecondName = secondName,
Password = passwd,
Email = email,
BirthDate = birthdate
})).Returns(new MwbeUser
{
UserName = userName,
Email = email,
FirstName = firstName,
SecondName = secondName,
BirthDate = birthdate
});
but it returns nulls. Why?
public void AddUserWithProperdata_UserIsAddedAndEmailIsGenerated()
{
// Arrange
String userName = "user";
String firstName = "Ala";
String secondName = "ADsadas";
String passwd = "passwd";
String email = "kot@wp.pl";
DateTime birthdate = DateTime.Today;
fakeUserService = A.Fake<IMwbeUserService>();
fakeAuthenticationService = A.Fake<IAuthenticationService>();
A.CallTo(() => fakeUserService
.AddUser(new MwbeUserRegistrationIn() { UserName = userName, FirstName = firstName, SecondName = secondName, Password = passwd, Email = email, BirthDate = birthdate }))
.Returns(new MwbeUser { UserName = userName, Email = email, FirstName = firstName, SecondName = secondName, BirthDate = birthdate });
MwbeUsersController controller = new MwbeUsersController(fakeUserService, fakeAuthenticationService);
MwbeUserRegistrationJson userjson = new MwbeUserRegistrationJson
{
username = userName,
passwd = passwd,
firstname = firstName,
secondname = secondName,
email = email,
birthdate = birthdate
};
// Act
IHttpActionResult untypedResult = controller.AddUser(userjson);
var test1 = untypedResult as OkResult;
var test2 = untypedResult as CreatedNegotiatedContentResult<MwbeUser>;
// Assert
Assert.IsNotNull(untypedResult);
}
public interface IMwbeUserService
{
MwbeUser AddUser(MwbeUserRegistrationIn regData);
...
}
UPDATE 1: added code of controller
[RoutePrefix("users")]
public class MwbeUsersController : ApiController
{
IMwbeUserService userSrv;
IAuthenticationService authService;
public MwbeUsersController(IMwbeUserService service, IAuthenticationService authSrv)
{
this.userSrv = service;
this.authService = authSrv;
}
...
[Route("register")]
[HttpPost]
public IHttpActionResult AddUser(MwbeUserRegistrationJson userdatadto)
{
// Validation
if (null == userdatadto)
{
return BadRequest("No user data");
}
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var registrationData = Conversion.ToUser(userdatadto);
if(registrationData.Code != MwbeResponseCodes.OK)
{
return BadRequest(registrationData.ErrorMessage);
}
// Registration
try
{
MwbeUser createdUser = userSrv.AddUser(registrationData.Data);
return Created<MwbeUser>("/users/" + createdUser.Id, createdUser);
}
catch (UserAlreadyExistsException)
{
return BadRequest("User with this username already exists");
}
catch (InvalidEmailAddress)
{
return BadRequest("Given e-mail address is invalid");
}
}
回答1:
I have little experience with FakeItEasy, but my guess is that your expectation is too strict.
The expectation on AddUser will only match if it is called with an object that is considered equal (as if by using object.Equals(object, object).
Since your controller passes in a different MwbeUserRegistrationIn object (registrationData):
.AddUser(new MwbeUserRegistrationIn() {
UserName = userName,
FirstName = firstName,
SecondName = secondName,
Password = passwd,
Email = email,
BirthDate = birthdate }))
and MwbeUserRegistrationIn presumably does not override object.Equals(object)
, this expectation will not be triggered. You can either implement MwbeUserRegistrationIn.Equals(object)
with value-based semantics or relax your expectations by something called argument constraints.
In your case, you could rewrite the expectation a bit:
A.CallTo(() => fakeUserService.AddUser(A<MwbeUserRegistrationIn>.That.Matches(u =>
u.UserName == userName &&
u.FirstName == firstName &&
u.SecondName == secondName &&
u.Password == passwd &&
u.Email == email &&
u.BirthDate == birthdate)))
.Returns(new MwbeUser
{
UserName = userName,
Email = email,
FirstName = firstName,
SecondName = secondName,
BirthDate = birthdate
});
来源:https://stackoverflow.com/questions/32067284/fakeiteasy-mocked-method-is-not-returning-expected-result