views:

37

answers:

2

I'd like to run some NUnit unit tests against a class in a .Net class library assembly which is designed to be hosted by an external process (outside of my control) and loaded at runtime.

The class I want to test derives from a class defined within the host exe that requires it to be instantiated within the host process. Any attempt to instantiate a derivative of the base class outside of the host process fails with an exception.

I am therefore not able to test the class in the NUnit gui or console test hosts by simply loading the assembly and instantiating the class. Does anyone have any suggestions as to how I can execute these tests?

A: 

In case when You want to test Your source and inside Your procedures You've those external class methods invokes, maybe mocking whole external process would solve You're problem.

You could design the mock to behave like Your host class.

Including:

  • mock loading runtime libraries (those from your scope of interest)
  • mock all possible output from particular method
  • mock exceptions

Since I'm not a .NET developer I'm not familiar with details, but I know that mocking engine in .NET is quite powerful.

bua
+2  A: 

I'd probably split the class into two classes - an 'outer' class that derives from the type in the external process, and an 'inner' class that does all the work, but does not inherit from anything that you do not control.

Then, test the inner class, and leave the outer class as devoid of logic as possible.

Unit testing is almost always easier when you can isolate your code from your dependencies in some fashion.

kyoryu
+1 This is the Humble Object pattern applied
Mark Seemann