c# - NSubstitute - TestFixture 1 causes AmbiguousArgumentsException in TestFixture 2 -



c# - NSubstitute - TestFixture 1 causes AmbiguousArgumentsException in TestFixture 2 -

i writing c# unit tests using nunit , nsubstitute. testing class effort retrieve objects config provider implementing next interface:

public interface iconfigprovider<t> { t getconfig(int id); t getconfig(string id); }

the class beingness tested uses int version of getconfig in setupfixture next set mocked config provider homecoming same dummy object:

iconfigprovider<configtype> configprovider = substitute.for<iconfigprovider<configtype>>(); configprovider.getconfig(arg.any<int>()).returns<configtype>(new configtype(/* args */);

this runs absolutely fine if testfixture 1 beingness run. however, in different testfixture in same assembly, check received calls this:

connection.received(1).setcallbacks(arg.any<action<message>>(), arg.any<action<long>>(), arg.any<action<long, exception>>());

if these received tests run before config provider tests, config tests fail in setupfixture ambiguousargumentsexception:

here.be.namespace.profilemanagertests+setup (testfixturesetup): setup : nsubstitute.exceptions.ambiguousargumentsexception : cannot determine argument specifications use. please utilize specifications arguments of same type. @ nsubstitute.core.arguments.nonparamsargumentspecificationfactory.create(object argument, iparameterinfo parameterinfo, isuppliedargumentspecifications suppliedargumentspecifications) @ system.linq.enumerable.<selectiterator>d__7`2.movenext() @ system.collections.generic.list`1..ctor(ienumerable`1 collection) @ nsubstitute.core.arguments.mixedargumentspecificationsfactory.create(ilist`1 argumentspecs, object[] arguments, iparameterinfo[] parameterinfos) @ nsubstitute.core.arguments.argumentspecificationsfactory.create(ilist`1 argumentspecs, object[] arguments, iparameterinfo[] parameterinfos, matchargs matchargs) @ nsubstitute.core.callspecificationfactory.createfrom(icall call, matchargs matchargs) @ nsubstitute.routing.handlers.recordcallspecificationhandler.handle(icall call) @ system.linq.enumerable.whereselectarrayiterator`2.movenext() @ system.linq.enumerable.firstordefault[tsource](ienumerable`1 source, func`2 predicate) @ nsubstitute.routing.route.handle(icall call) @ nsubstitute.proxies.castledynamicproxy.castleforwardinginterceptor.intercept(iinvocation invocation) @ castle.dynamicproxy.abstractinvocation.proceed() @ castle.proxies.iconfigprovider`1proxy.getconfig(int32 id) @ here.be.namespace.profilemanagertests.setup.dosetup()

what's confusing me can observe effect between test runs - if utilize nunit gui run received tests alone, , run config tests alone, config tests fail. if run config tests again, pass.

things i've tried:

adding configprovider.getconfig(arg.any<string>()).returns... well, in case overloading problem. i've read nsubstitute docs on argument matching, can't find solution there. if case of having supply argument matchers both int , string versions of method, can't work out how that.

as happens, tests i'm using ever phone call getconfig method values of 0 or 1, can provide returns specifications 2 values , not utilize matching @ all, want understand how prepare more generally.

ambiguous arguments when nsubstitute compares arguments phone call working with, stack of "argument matchers" has (each time arg.blah called, argument matcher added stack), , unable resolve argument goes where.

normally caused having phone call blah(null, null), single argument matcher queued up, can caused stack getting out-of-sync due arg matcher beingness used outside of phone call configuration, or argument non-virtual method.

version 1.8.0 (released after question) includes improved detection of latter case, may worth trying.

other that, i've had problem few times , have used next (painful) approach.

run test in isolation , ensure passes work out test runs proceeding (can guess, test logs can help here), , run 2 tests. confirm fails. look calls arg.xyz queue argument matcher in either test. create sure used part of phone call configuration. working out phone call problematic can done commenting out lines or replacing arg matchers other values. make sure there no calls non-virtual methods confusing nsubstitute.

sometimes problem may due previous fixture, may need workout previous fixture , explore there well. :(

c# nunit nsubstitute

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -