entity-framework-core-mock
entity-framework-core-mock copied to clipboard
Moq request: Allow overriding previously set mocked dbset
DbContextMock.CreateDbSetMock has the following line:
if (_dbSetCache.ContainsKey(entityType)) throw new ArgumentException($"DbSetMock for entity {entityType.Name} already created", nameof(dbSetSelector));
which effectively shuts off any attempt to override an existing set. There is no precedent for this in Moq; the following will execute without an exception and print "6".
void Main()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Bar()).Returns(5);
mock.Setup(x => x.Bar()).Returns(6);
Console.WriteLine(mock.Object.Bar());
}
public interface IFoo
{
int Bar();
}
My use case is I have a utility function that sets empty result sets for several different DbSets that's called in most test functions to create a default state, and then if the test specifically uses one DbSet then I override it there. I've created a workaround, but I also don't see a compelling reason to keep this behavior (or at least not make it configurable).