Chris Meek
Chris Meek

Reputation: 5849

Mocking System.Drawing.Image with Moq

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 testableObject = new ObjectUnderTest(image.Object);

testableObject.MethodBeingTested();

image.VerifyAll();

Upvotes: 1

Views: 4574

Answers (1)

johnny g
johnny g

Reputation: 3561

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]

Upvotes: 5

Related Questions