1

I want to create a mock of ImageSource for unit test purposes. ImageSource has an internal constructor and I can't neither create a mock nor create an inherited class. As is explained here for brushes, but it also fits for ImageSource: How do I implement a custom Brush in WPF?

Is there any workaround for that?

Community
  • 1
  • 1
0xddr
  • 65
  • 1
  • 8

1 Answers1

2

If I understand your situation correctly you want to create a mock for a sealed class with no public constructor.

You can't do this with Moq.

As a workaround you coudl use other "mocking/isolation" frameworks, ie: TypeMock, which supports this feature. You may also have luck with Microsoft Moles.

On the other hand, if you are trying to inject a mocked instance of ImageSource in a framework class, that would be a smell that something may be wrong with your test.

At the very least, you could abstract away the framework class with another class that you could mock, removing the need for your this to use this class and ImageSource directly.

Gilles
  • 5,269
  • 4
  • 34
  • 66
  • I'm forced to use Moq. I test my own class, not framework one. Abstracting away from ImageSource seems to be quite good way to solve my problem. Thanks. – 0xddr May 17 '12 at 12:39