I am using Entity Frameowrk 4.0 and I am calling a stored procedure which returns an ObjectResult and I tried to use MOQ and have not been able to mock ObjectResult. Has anybody been able to mock ObjectResult using moq?
TIA Yaz
I have this problem too; I'm using database-first design, and the EF 4.x DbContext Generator template to generate my DbContext.
I'm modifying the context generator in the following ways:
ObjectResult (according to the MSDN docs) is a sealed class as such you cannot mock it. The way Mocking libraries like Moq work is that when you do something like
Mock<Foo> fooMock = new Mock<Foo>();
It generates (using Reflection.Emit and various other magic tricks) a class that looks a little like this
public class FooMockingProxy : Foo {
public override void This() {
// Mocking interceptors to support Verify and Setup
}
public override string That() {
// Mocking interceptors to support Verify and Setup
}
}
i.e. It takes the class (interface) you want to Mock and subclasses it (or implements it in the case of an interface). This allows it to put in instrumentation that allows it to check if a method has been called, or returns a certain value (this supports the various Setup and Verify methods). The restrictions to this method of mocking are:-
One technique you can take when approaching sealed classes is to wrap them in some kind of interface that is Mockable. Alternatively you can try and Mock an interface that the sealed class implements that only your code consumes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With