0

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.
        }
    }
Adam Ralph
  • 29,453
  • 4
  • 60
  • 67
Joy
  • 1,707
  • 7
  • 29
  • 44
  • 5
    Unless that test is missing some code I think you've missed the point. You're not meant to call the methods on the fake directly in your test, you're meant to pass the fake in to the class you're testing as a dependency so it can be used in place of a real dependency. If you were using it as a dependency, you'd have to specify your result. Take a look at http://fakeiteasy.github.io/, the simple example on the home page shows enough to get you started. – DoctorMick Dec 12 '14 at 11:56
  • `GetSupplierById` method should be initialised before its invocation. – Ilya Palkin Dec 17 '14 at 15:05

1 Answers1

0

Before calling any actual function call you need to make sure to call all the internal fake call like below

 A.CallTo(() => fakeRepository.GetclientById(expectedclient)).WithAnyArguments().Returns(Fakeobject/harcoded object);

Then go for the unit test call

 var fakeResult = fakeRepository.GetSupplierById(expectedSupplier); 

after that go for MustHaveHappened/ MustNotHaveHappened/Equals

A.CallTo(() => fakeRepository.GetclientById(expectedclient)).MustHaveHappened();
A.CallTo(() => fakeRepository.GetSupplierById(expectedSupplier).IdSupplier).Equals(expectedSupplier);

The Implementation should be like this

[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)).WithAnyArguments().Returns(Fakeobject/harcoded object);
                var fakeResult = fakeRepository.GetSupplierById(expectedSupplier); // returns null
        A.CallTo(() => fakeRepository.GetclientById(expectedclient)).MustHaveHappened();
                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.
            }
        }
Sudipto Sarkar
  • 346
  • 2
  • 11