开发者

Mocking System.Drawing.Image with Moq

开发者 https://www.devze.com 2022-12-09 08:05 出处:网络
How would I go about mocking an Image with Moq? It has no constructors (is always generated from factory methods).

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]

0

精彩评论

暂无评论...
验证码 换一张
取 消