c# - How to get Moq to verify method that has an out parameter -


i have interface definition method has out parameter defined

public interface irestcommunicationservice {     tresult performpost<tresult, tdata>(string url, tdata datatosend, out standarderrorresult errors); } 

i have following class using above interface

    public tripcreatedispatchservice(irestcommunicationauthservice restcommunicationservice, isettings settings)     {         _restcommunicationservice = restcommunicationservice;         _settings = settings;     }      public flightalert createtrip(string consumernumber, postalertmodel tripmodel, out standarderrorresult apierrors)     {         url = .. code ommited         var result = _restcommunicationservice.performpost<flightalert, postalertmodel>(url), tripmodel, out apierrors);         return result;     } 

in unit tests trying verify performpost method of restcommunication object called.

but no matter do, cannot moq verify method called

    public void dispatchservice_performpost()     {         var consumernumber = ...         var trip = ...         var result = ...         var apierrors = new standarderrorresult();         ... code setup mock data         _mockrestcommunicationservice = new  mock<irestcommunicationauthservice>();         _mockeestcommunicationservice.setup(x => x.performpost<string, postalertmodel>(it.isany<string>(), it.isany<postalertmodel>(), out apierrors)).verifiable();           _systemundertest.createtrip(consumernumber, trip, out apierrors);          _mockrestcommunicationservice.verify(m =>              m.performpost<standarderrorresult, postalertmodel>(             it.isany<string>(),              it.isany<postalertmodel>(), out apierrors             ), times.once);     } 

but receiving following error

moq.mockexception :   expected invocation on mock once, 0 times:  m => m.performpost<standarderrorresult,postalertmodel>(it.isany<string>(), it.isany<postalertmodel>(), .apierrors)  no setups configured. 

how go verifying method called.

i using moq , nunit

update 1

as per comment sunny, have modified test use callback follows

var consumernumber = ... var trip = ... var result = ... standarderrorresult apierrors; _mockrestcommunicationservice.setup(x => x.performpost<string, postalertmodel>(             it.isany<string>(), it.isany<postalertmodel>(), out apierrors))     .callback<string, postalertmodel, standarderrorresult>     ((s, m, e) => e.errors = new system.collections.generic.list<standarderror>()     {         new standarderror { errorcode = "code", errormessage = "message" }     });  _systemundertest.createtrip(consumernumber, trip, out apierrors); assert.that(apierrors.errors, is.not.null); 

this error being thrown when executing test.

system.argumentexception : invalid callback. setup on method  parameters (string,postalertmodel,standarderrorresult&)  cannot invoke callback parameters (string,postalertmodel,standarderrorresult).    @ moq.methodcall.throwparametermismatch(parameterinfo[] expected, parameterinfo[] actual)    @ moq.methodcall.setcallbackwitharguments(delegate callback)    @ moq.methodcallreturn`2.callback(action`3 callback) 

this error thrown @ setup statement.

fyi. using resharper 8 , using test runner execute tests.

if try add out parameter callback, code not compile.

i same error if modify setup

_mockrestcommunicationservice.setup(x => x.performpost<string, postalertmodel>( it.isany<string>(), it.isany<postalertmodel>(), out apierrors)) .callback     ((string s, postalertmodel m, standarderrorresult e) => e.errors = new system.collections.generic.list<standarderror>()         {             new standarderror { errorcode = "code", errormessage = "message" }         }); 

it's better use aaa , not verify mock. setup method return specific result , assert result have been returned:

var mycommresult = new postalertmodel(); _mockeestcommunicationservice     .setup(x => x.performpost<string, postalertmodel>(it.isany<string>(), it.isany<postalertmodel>(), out apierrors)     .returns(mycommresult);  var response = _systemundertest.createtrip(consumernumber, trip, out apierrors);  assert.aresame(mycommresult, response); 

the above validate method called, based on example code.

if reason, code in question not representative of real code, , there no way assert on return of method, can use callback instead, , put in errors can verify.

something like:

_mockeestcommunicationservice    .setup(x => x.performpost<string, postalertmodel>(it.isany<string>(), it.isany<postalertmodel>(), out apierrors))    .callback( (string s, postalertmodel m, standarderrorresult e) => e.add("some error test"); 

and later verify apierrors has error inserted in callback.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -