chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
I have a set of unit tests I run for some Mapper objects that use the .NET Entity Framework, and I think I may have a mocked entity being automatically reused. All tests run fine on their own, but when I run all tests in the solution some tests fail. I found that running certain tests in a certain order would cause failures.

For example, I have a Delete test, a Load test, and an Update test. They all succeed when run separately, but when run in "Delete, Load, Update" order the Update test fails.

After troubleshooting further, I found that the fake entity I create in the Load test is actually being used again in the Update test, even though they are entirely different tests! Here's a code snippet showing how I'm mocking entities:

    using (fakeEntities = Isolate.Fake.Instance<EmeraldsEntitiesContainer>(Members.ReturnRecursiveFakes))
    {
        fakeEntities.ContextOptions.ProxyCreationEnabled = false;
        fakeObjectQuery = Isolate.Fake.Instance<ObjectQuery<Ccsds>>(Members.ReturnRecursiveFakes);
        fakeCcsds = Isolate.Fake.Instance<Ccsds>(Members.MustSpecifyReturnValues);
        Isolate.Swap.NextInstance<EmeraldsEntitiesContainer>().With(fakeEntities);
        Isolate.WhenCalled(() => fakeEntities.CcsdsSet).WillReturn(fakeObjectQuery);
        Isolate.WhenCalled(() => fakeObjectQuery.FirstOrDefault<Ccsds>()).WillReturn(fakeCcsds);

        ...

    }


The fake entity is created nearly the same way in both tests (in the Update test, I use Members.CallOriginal instead of Members.MustSpecifyReturnValues).

I think I may be a victim of some built-in caching the Entity Framework is doing for performance reasons. I've tried adding some fakeEntities.Detach calls and some GC.Collect calls, but haven't had any luck yet. Does anybody know a way to prevent this entity caching? Thanks in advance.

- Dave
asked by daviddavidson3 (1.3k points)

2 Answers

0 votes
Hi,

Are you using the [Isolated] attribute around your tests? this should prevent faked behavior from leaking between tests. If this is not the case, this indeed may be some EF mechanism at work, and I'll need to investigate further.

Another tip - the 'using' clause is redundant - the Dispose() will be faked because this is a fake object.

Doron
Typemock support
answered by doron (17.2k points)
0 votes
Doron,

I was not using the Isolated attribute, and adding it solved my problem. Thank you very much for your help. Also, thank you for the tip. All my tests are now working.

- Dave
answered by daviddavidson3 (1.3k points)
...