I made a repository class to access my DB and then I made a unit test using the FakeItEasy library. Using a real repository I got the expected result, while using the fake repository returns null.
[TestClass]
public class clientRepositoryTest
{
[TestMethod]
public void GetclientById()
{
var fakeRepository = A.Fake<IclientRepository>();
const int expectedclient = 1;
IclientRepository realRepository = new clientRepository();
var realResult = realRepository.GetclientById(expectedclient); // returns expected object
try
{
A.CallTo(() => fakeRepository.GetclientById(expectedclient)).MustHaveHappened();
var fakeResult = fakeRepository.GetSupplierById(expectedSupplier); // returns null
A.CallTo(() => fakeRepository.GetSupplierById(expectedSupplier).IdSupplier).Equals(expectedSupplier);
}
catch (Exception ex)
{
//The current proxy generator can not intercept the specified method for the following reason:
// - Non virtual methods can not be intercepted.
}
}
Before calling any actual function call you need to make sure to call all the internal fake call like below
Then go for the unit test call
after that go for MustHaveHappened/ MustNotHaveHappened/Equals
The Implementation should be like this