0

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

4
  • If you want to write unit tests everything should be mocked except the class under test. So why do you need real implementation of UnitOfWork? and the second the implementation of unit of work you are using is not good every time you add new repository you will need to change unit of work class Check out my implementation of UnitOfWork github.com/exiton3/AppFactory/blob/master/Source/… Commented May 28, 2024 at 16:24
  • you need to mock this method(property) in UnitOfWork unitOfWork.TblageeventiRepo to return fake implementation of repository Commented May 28, 2024 at 16:26
  • Is the TblageeventiRepo property virtual? If not, FakeItEasy can't fake it. Commented May 28, 2024 at 20:26
  • I thought i had to mock the property with unitofwork and not separately. Now that i mocked it (only the repo) seems to work. Thank you Commented May 29, 2024 at 7:28

1 Answer 1

0

your error it says that you need first mock the property of UnitOfWork

unitOfWork.TblageeventiRepo

to return a fake implementation of repository and only then to mock methods from repository

Sign up to request clarification or add additional context in comments.

1 Comment

Ok thank you, i thought that i had to mock TblageeventiRepo with unitOfWork, but i mocked it separately and works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.