Unit testing of a third-party API with private concrete classes

TDD had just begun, and everything was going fine until I hit this brick wall.

I am writing a facade around a third-party API. The API is pretty nice in that everything is accessible through interfaces, so it’s easy to test when testing my facade.

The entire API is accessible through the root interface, and there is a deep hierarchy of interfaces that you can handle. My facade uses this root interface in its constructor in standard IoC practice.

TDD went well, with a slight pain, that the mockery was a bit complicated when using deep interfaces, since I have to mock the entire interface tree. However, despite this, I simply supported the helper function, which built the layout. It really makes me wonder if I am using the right approach correctly.

Anyway, halfway down the tree I suddenly fell into a sealed concrete type, without a public constructor, so I could not taunt him. This causes my tests to fail, because the mocking API always returns null for this element.

The only way I can see this is to create my own interface for this type and have a virtual method on my facade to access it. However, this seems useless to me, as I have no way to provide access to the type using this method, and it will be easy to forget. For example, it is natural to use:

ConcreteType c = SomeInterface.ConcreteMember;

instead:

IConcreteType c = GetConcreteMember(SomeInterface);

Forgetting this will cause the test to fail.

Did I miss something fundamental? As I said, I am very new to unit testing.

ps. I am using Moq.

+3
source share
1 answer

Use moles . Using this, you can create mocks for any private type (I myself used it recently to provide mocks from the System.Web.HttpContext.Current property).

(Microsoft Moles Reference Manual.docx), , .

+2

All Articles