I've been able to mock DbSet
's from entity framework with Moq using this link.
However, I would now like to know how I could mock the call to SqlQuery. Not sure if this is possible or how as it relies on the mocked db context knowing what "query" is being called.
Below is what I am trying to mock.
var myObjects = DbContext.Database
.SqlQuery<MyObject>("exec [dbo].[my_sproc] {0}", "some_value")
.ToList();
I currently haven't tried anything as did not know how to start mocking this example.
The mocking of the DbSet
is below and to re-iterate, I can correctly mock returning a DbSet
of MyObject
's but now am trying to mock a SqlQuery that returns a list of MyObject
's.
var dbContext = new Mock<MyDbContext>();
dbContext.Setup(m => m.MyObjects).Returns(mockObjects.Object);
dbContext.Setup(m => m.Database.SqlQuery... something along these lines