How would I go about mocking an Image with Moq?
It has no constructors (is always generated from factory methods).
Basically I want to do something like this...
var image = new Mock<Image>();
image.Setup(i=>i.Save(someStream,ImageFormat.Jpeg);
var testableObje开发者_如何转开发ct = new ObjectUnderTest(image.Object);
testableObject.MethodBeingTested();
image.VerifyAll();
Answered a similar question yesterday, you may wish to take a look at this thread on mocking a static Singleton.
If it's absolutely necessary to mock this behaviour, then the proscribed method is to generate an interface exposing the methods you need, implement it with a concrete class [effectively wrapping Image class] for prod, and Mock the interface for test.
It does sound overkill, but if you need to Mock\verify the interactions between your testable class and Image, that's the way to do it. Alternatively, you can just pass your testable class an actual instance of Image and then compare this instance against an expected output [standard unit test methodology]
精彩评论