In a PUT route, I have an utility method where we put the business logic (so we can call the method and not the route when we need to do the job that route does).
I don't understand how can I fake the data connection.
In the route, I call
using (var unitOfWork = new UnitOfWork(IP, DbName, UserId, IdAzienda))
{
GroupEvents ret = UtilityOffPrenotazioni.UpdatePrenotazione(event, unitOfWork, group, eventId, userId, mapper, UserId, agencyId);
}
Inside the function i do some validation and other things, including some db operations like
Tblageeventi ev = new Tblageeventi();
ev = unitOfWork.TblageeventiRepo.Read(eventId, userId)
...
...
ev = unitOfWork.TblageeventiRepo.Update(ev);
eventRet = mapper.Map<Tblageeventi, EventiGruppo>(ev);
unitOfWork.TblageeventiRepo.Delete(new Tblageeventi()
{
UserId = userId,
EventId = eventId
});
In the test method, I have something like this
var retEvent = A.Fake<Tblageeventi_extended>();
var ev = A.Fake<Tblageeventi>();
using (var unitOfWork = A.Fake<UnitOfWork>((x => x.WithArgumentsForConstructor(() => new UnitOfWork(_ip, _dbName, _userId, _agencyId, "", 5432)))))
{
A.CallTo(() => unitOfWork.TblageeventiRepo.Read(eventId, userId)).Returns(retEvent);
A.CallTo(() => unitOfWork.TblageeventiRepo.Update(ageeventi, "")).Returns(ev);
// Act
UtilityOffPrenotazioni.UpdatePrenotazione(groupEvents, unitOfWork, groups, eventId, idUtente, _mapper, _userId, _agencyId);
}
The error says
Object 'SxMultitenantDAL.Repos.TblageeventiRepository' of type SxMultitenantDAL.Repos.TblageeventiRepository is not recognized as a fake object
I don't find a way to fake TblageeventiRepo (its type is TblageeventiRepository).
For more information, read signature inside the TblageeventiRepository class ->
public Tblageeventi_extended Read(int eventId, int userId).
Is there a way for mocking this? I am starting now studying testing topic, so I don't know even if the way I wrote (organize) is correct.
Thank you
TblageeventiRepoproperty virtual? If not, FakeItEasy can't fake it.