I wanted to mock an entityframwork DbSet
using Foq. It goes something like:
let patients =
([
Patient(Guid "00000000-0000-0000-0000-000000000001");
Patient(Guid "00000000-0000-0000-0000-000000000002");
Patient(Guid "00000000-0000-0000-0000-000000000003");
]).AsQueryable()
let mockPatSet = Mock<DbSet<Patient>>.With(fun x ->
<@
// This is where things go wrong. x doesn't have a property Provider
x.Provider --> patients.Provider
@>
)
I tried coercing and casting x
to an IQueryable
at some places but that doesn't work.
As you can see here in the docs for DbSet
it does implement the IQueryable
interface via DbQuery
, but does so by "explicitly" implementing the properties.
In Moq there is a Function As
so you can tell it to treat it as a IQueryable
that looks like:
var mockSet = new Mock<DbSet<Blog>>();
mockSet.As<IQueryable<Blog>>().Setup(m => m.Provider).Returns(data.Provider);