I am trying to find a good solution for this, but no luck, so either I am not looking for suitable keywords, or we are doing something wrong from the very beginning, so the problem should not really exist.
Update for clarification: I would like this to work as a unit test, and not as an integration test, so I don't want this to get into the database, but I want to make fun of associations when EF saves the changes to my unit test.
The original question:
Suppose you are testing a service method as follows:
[Test] public void AssignAuthorToBook_NewBookNewAuthor_SuccessfullyAssigned() { IBookService service = new BookService(); var book = new Book(); var Author = new Author() { Id = 123 }; service.AssignAuthorToBook(book, author); Assert.AreEqual(book.AuthorId, 123); }
Now let's say that this test failed because AssignAuthorToBook really works using the code book.Author = author; therefore it does not assign AuthorId , it assigns an entity. When this is saved using the Entity Framework SaveChanges() method in the context, it will bind entities and identifiers will correlate. However, in my example above, the logic of the Entity Framework would not be applied. I say that the code will run once when SaveChanges() was called, but the unit test will fail.
In this simple example, you will probably immediately find out why your test failed because you just wrote the test immediately before the code and could easily fix it. However, for more complex operations and operations in which future changes may change the way communications between objects occur, which will lead to test disruptions, but may not disrupt functionality, how is unit testing best suited?
My thoughts:
- The level of service should be unfamiliar with the level of persistence. Should we mock the data context in unit tests in order to mock how it works? Is there an easy way to do this that will automatically associate the associations (i.e. Assign the correct entity if
Id used or assign the correct Id if the object is used)? - Or should tests be structured somewhat differently?
Those tests that exist in the current project that I inherited work as in my example above, but it turns out to me that something is wrong with this approach, and that I could not find a simple solution for a possible common problem . I believe that the data context should be mocked, but it looks like you need to add a lot of code to the layout to dynamically create associations - has this been decided for sure?
Update: These are the closest answers I have found so far, but they are not quite what I need. I donβt want to test EF as such, I just wondered what is best suited for testing service methods that access repositories (either directly or through navigation properties through other repositories that use the same context).
How can I redo the Intelligent Intelligent Intelligent Integral (Entity Framework)?
Fixed datacontext and foreign keys / navigation properties
Fake DbContext for Entity Framework 4.1 for testing
Navigation Properties Not Set Using ADO.NET Mocking Context Generator
Conclusion:. This is not possible with the help of unit testing and is only possible with the help of testing integration with a real database. You can come close and probably encode something to dynamically associate navigation properties, but your data layout will not fully replicate the real context. I would be glad if any solution allowed me to automatically bind navigation properties that would allow my unit tests to be better, if not perfect (the nature of a successful unit test in no way guarantees functionality) ADO.NET Mocking Context Generator is close, but it seems that I will have to have a mock version of each entity that will not work for me in case functioanlity is added to them using partial classes in my implementation.