c# - Extracting arguments from received call and assert on them -


how can assertions on arguments received call? below example not work, because action passed arg.do() never called.

ienumerable<tuple<string, string>> receivedlargs = null; provider.received(1)     .setvaluesasync(arg.do<ienumerable<keyvaluepair<string,object>>>(     args =>     {         receivedlargs = args.select(a => new tuple<string, string>(a.key, a.value.tostring()));     }));  // assert (using fluentassertions - example) receivedlargs.should().equal(tuple.create("key1", "foo"), tuple.create("key2", "bar")); 

the standard approach assert call correct arguments received, rather capturing argument , asserting on it.

provider.received(1)     .setvaluesasync(arg.is<ienumerable<keyvaluepair<string,object>>>(     pairs => sameelements(pairs, new [] { tuple.create("key1", "foo"), ... }))); 

you can extract more or less of code different methods make more readable.

the problem approach doesn't give information differences between arguments fluentassertions. in cases can fall when..do or arg.do capture argument per question. in cases don't want use received, stub out call perform particular action whenever called. make sure setting do callback before method being tested called.

//arrange ienumerable<keyvaluepair<string, object>> receivedargs = null; provider.setvaluesasync(   arg.do<ienumerable<keyvaluepair<string,object>>>(x => receivedargs = x) ); // act: callwearetesting(); // assert: receivedargs.should().... 

or can use when..do syntax:

provider.whenforanyargs(x => x.setvaluesasync(null))         .do(x => receivedargs = x.args<ienumerable<keyvaluepair<string,object>>>()); 

Comments