c# - How to structure unit tests when you want to test interactions of derived classes wih base class -
i started organizing unit tests in way phil haack suggested here. test classes have 1 nested class per public method under test. nested classes derived outer class in order inherit setup code.
i have case want test simple hierarchy of 1 abstract base class , 2 derived classes. test interactions of each derived class base class explicitly rather testing base class kind of mock derived class.
i've done in past using base test fixture , 1 derived fixture per derived class derived fixtures have implement few template methods tests in base fixture:
[testfixture] public abstract class basefixture { protected abstract mybaseclassundertest gettestinstance(); [test] public void somemethod_somecondition_someoutcome() { var sut = gettestinstance(); //test base class behavior here } //more base class tests here } public class derivedfixture : basefixture { protected override mybaseclassundertest gettestinstance() { return new derivedinstance(); } //tests derived class go here }
does have idea, how can resolve this? right can't see, how can combine 2 approaches, because of different inheritance strategies (inheriting setup code outer class versus inheriting base class tests base class fixture).
it's interesting problem, of course multiple inheritance isn't possible therefore guess need find alternative 1 of 2 inheritance strategies using.
personal opinion should keep haack method of organizing tests consistency across test suite, , alternative approach sharing tests through class hierarchy. closest concept know multiple inheritance of mixins
, , if @ answer is possible implement mixins in c# appears mixins can simulated using extension methods. go down path - this blog post discusses possible strategies creating mixins can access data held in class uses them, require believe able supply derived class instance base class level tests (i.e. replacing gettestinstance
).
Comments
Post a Comment