c# - How to spy the class under test with AutofacContrib.NSubstitute -


i'm running unit tests in class library project nspec framework, autofaccontrib.nsubstitute v3.3.2.0, nsubstitute v1.7.0.0 (the latest of 1.8.2).

the class under test instance built autosubstitute, in order automock needed dependencies.

autosubstitute autosubstitute = new autosubstitute();  mainpanelviewmodel viewmodel = autosubstitute.resolve<mainpanelviewmodel>(); 

if working properly, class under test @ point invoke 1 of it's base class methods specific input parameter (the base class out of control):

// ... base.activateitem(nextscreen); // ... 

so, test expectation, need check (spy) instance invokes base method:

viewmodel.received().activateitem(arg.any<somespecificscreentype>()); 

here's problem: when try this, @ runtime nsubstitute complains can ran received() against object created substitute.for<>(). checked autofaccontrib.nsubstitute source code, not find way obtain instance automocking , @ same time wrap somehow in spy object or that.

i thought maybe substitute.forpartsof<>() helpful, method not seem found in nsubstitute v1.7.0.

for sake of completeness, here's nsubstitute full error:

nsubstitute extension methods .received() can called on objects created using substitute.for() , related methods.

for completeness, did experimenting nsubstitute's partial substitutions forpartsof.

the way forpartsof works define new class inherits class you're using template. restricts mocking framework can intercept methods either defined abstract or virtual. same limitation have modifying behaviour of class if inherit own class.

taking information, lets @ problem. want intercept call:

base.activateitem(nextscreen); 

so, because of limitations above, able intercept call activateitem, method has marked virtual in base class. if it's not, there's nothing can without changing application structure.

if method marked virtual, can intercept nsubstitute but can if nsubstituted implementation called. works through normal method dispatch, because highest level implementation of virtual method called (the 1 provided nsubstitute) when invoke it. however, doesn't work when you're calling method via base reference.

so, whilst intercept this:

activateitem(nextscreen) 

you can't intercept this:

base.activateitem(nextscreen); 

the fact you're using base.activateitem in code suggests class under test has own implementation of method don't want call, current tools can't achieve trying do. why it's thing found workaround.

you're in same situation other mocking frameworks, including moq. exception typemock, uses totally different way intercept method calls means can things other frameworks can't.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -