jeudi 25 juin 2015

How to spy the class under test with AutofacContrib.NSubstitute

I'm running unit tests in a class library project with NSpec framework, AutofacContrib.NSubstitute v3.3.2.0, NSubstitute v1.7.0.0 (the latest as of now is 1.8.2).

The Class Under Test instance is built with AutoSubstitute, in order to automock all its needed dependencies.

AutoSubstitute autoSubstitute = new AutoSubstitute();

MainPanelViewModel viewModel = autoSubstitute.Resolve<MainPanelViewModel>();

If working properly, my Class Under Test at some point will invoke one of it's base class methods with some specific input parameter (the base class is out of my control):

// ...
base.ActivateItem(nextScreen);
// ...

So, for test expectation, I need to check (spy) that the instance invokes the base method:

viewModel.Received().ActivateItem(Arg.Any<SomeSpecificScreenType>());

Here's the problem: when I try to do this, at runtime NSubstitute complains that I can only ran Received() against an object created with Substitute.For<>(). I also checked quickly AutofacContrib.NSubstitute source code, but I could not find a way to obtain the instance with automocking and at the same time wrap it somehow in a spy object or something like that.

I also thought that maybe Substitute.ForPartsOf<>() could be helpful, but that method does not seem to be found in NSubstitute v1.7.0.

For sake of completeness, here's NSubstitute full error:

NSubstitute extension methods like .Received() can only be called on objects created using Substitute.For() and related methods.

Aucun commentaire:

Enregistrer un commentaire