I'm mocking a DbContext to auto-test classes that update the database.
I'm using Entity Framework Core and .NET 5.
Mock<MyDbContext> fakeDbContext = new Mock<MyDbContext>();
List<Book> availableBooks = new List<Book>(); // filled elsewhere
fakeDbContext.Setup(ct => ct.BookRepository).ReturnsDbSet(availableBooks);
This works fine, up to
fakeDbContext.BookRepository.Add(newBook);
The data from newBook show neither in BookRepository not in availableBooks.
So I guess I have to mock Add() further (I thought Setup().ReturnsDbSet() handled that, mistakenly it appears).
That would be the easiest way to call availableBooks.Add(). However, it has to return an EntityEntry<Book> object, which I haven't yet found how to create (it has a constructor, but not recommended for regular use).
I can't find any documentation on the matter, however. What can I try next?
MockQueryablefor DB operations. I'm going to give it a try.