Ask yourself one question: what are you going to test?
You mentioned FakeContext and are mocking the context - why use both? These are just different ways to do the same thing - provide only a test implementation of the context.
There is another big problem - falsification or a mocking context or set has only one result: you are no longer testing your real code.
A simple example:
public interface IContext : IDisposable { IDbSet<MyEntity> MyEntities { get; } } public class MyEntity { public int Id { get; set; } public string Path { get; set; } } public class MyService { private bool MyVerySpecialNetMethod(e) { return File.Exists(e.Path); } public IEnumerable<MyEntity> GetMyEntities() { using (IContext context = CreateContext()) { return context.MyEntities .Where(e => MyVerySpecialNetMethod(e)) .Select(e) .ToList(); } } }
Now imagine that this is in your SUT (the system is under testing - in the case of unit test it is unit = usually a method). In the test code, you provide FakeContext and FakeSet , and it will work - you will have a green test. Now in the production code, you will provide another DbContext and DbSet , and you will get an exception at runtime.
Why? Because, using FakeContext , you also changed the LINQ provider, and instead of LINQ to Entities you use LINQ to Objects, so you call local .NET methods that cannot be converted to SQL, as well as many other LINQ functions that are not available in LINQ to entities ! There are other problems that you may find with changing data: referential integrity, cascading deletes, etc. That is why I believe that the code associated with the / LINQ to Entities context should be covered by integration tests and executed with a real database.
Ladislav Mrnka Jul 20 2018-11-21T00: 00Z
source share