Im using FakeItEasy to mock methods for unit tests. One of method (using REF parameter - maybe this is important) saves data in database, so it's mocked as 'Does Nothing'
A.CallTo(() => mockedUserRepository.Save(ref mwbeUserData)).DoesNothing();
. But in this situation last assert fails
A.CallTo(() => mockedUserRepository.Save(ref mwbeUserData)).MustHaveHappened(Repeated.Exactly.Once);
with error:
Assertion failed for the following call:
MobileWallet.Common.DAL.IMwbeUserRepository.Save(<NULL>)
Expected to find it exactly once but found it #0 times among the calls:
1: MobileWallet.Common.DAL.IMwbeUserRepository.Get(userName: \"AAA\")
2: MobileWallet.Common.DAL.IMwbeUserRepository.Save(user: MobileWallet.Common.Repository.MwbeUserData)
Full code of test
[TestMethod]
public void AddUser_MwbeUserObjectReturned()
{
//Arrange
MwbeUserRegistrationIn userRegistrationIn = new MwbeUserRegistrationIn()
{
BirthDate = DateTime.Today,
Email = "www@wp.pl",
FirstName = "Adam",
SecondName = "Ada2",
UserName = "AAA"
};
//mock mockedNotificationService and related:
INotificationService mockedNotificationService = A.Fake<INotificationService>();
//TODO: create notofication service
//mock IMwbeUserRepository and related
IMwbeUserRepository mockedUserRepository = A.Fake<IMwbeUserRepository>();
MwbeReturnData<MwbeUserData> mwbeReturnData = new MwbeReturnData<MwbeUserData>(MwbeResponseCodes.NotFound);
MwbeUserData mwbeUserData = mwbeReturnData.Data;
A.CallTo(() => mockedUserRepository.Get(userRegistrationIn.UserName)).Returns(mwbeReturnData);
A.CallTo(() => mockedUserRepository.Save(ref mwbeUserData)).DoesNothing();
MwbeUserService userService = new MwbeUserService(mockedUserRepository, mockedNotificationService);
//Act
MwbeUser user = userService.AddUser(userRegistrationIn);
//Assert
Assert.IsNotNull(user);
Assert.AreEqual(userRegistrationIn.Email, user.Email);
Assert.AreEqual(userRegistrationIn.UserName, user.UserName);
Assert.AreEqual(userRegistrationIn.FirstName,user.FirstName);
Assert.AreEqual(userRegistrationIn.SecondName, user.SecondName);
Assert.AreEqual(userRegistrationIn.BirthDate, user.BirthDate);
A.CallTo(() => mockedUserRepository.Get(userRegistrationIn.UserName)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => mockedUserRepository.Save(ref mwbeUserData)).MustHaveHappened(Repeated.Exactly.Once);
}
UPDATE 1:
Please notice that for both calls to this method Save, value of mwbeUserData is the same
MwbeUserData mwbeUserData = mwbeReturnData.Data;
If this is null (but it's not).
Maybe the is problem with syntax for REF parameter? I read that there should be used method called AssignsOutAndRefParameters, but I dont know exactly how to use it.
For now I will use MATCHES parameter to make it more generic.