c# - Unit test failing assertion in async unit test -
i trying unit test async code. using nunit switch xunit can't tests working. here example code working nunit tests simplified sake of asking question:
[testfixture] public class tests { [test] public async task usingasynctask() { // arrange pubsubevent<object> pubsubevent = mock.create<pubsubevent<object>>(); // act await task.run(() => { pubsubevent.publish(new object()); }); // assert mock.assert(() => pubsubevent.publish(null), args.ignore(), occurs.once()); } }
add following nuget packages.config:
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="justmock" version="2015.1.224.3" targetframework="net45" /> <package id="nito.asyncex" version="3.0.0" targetframework="net45" /> <package id="nunit" version="2.6.4" targetframework="net45" /> <package id="nunittestadapter" version="2.0.0" targetframework="net45" /> <package id="prism.pubsubevents" version="1.1.2" targetframework="net45" /> </packages>
all of these unit tests pass without problem. try same thing in xunit:
public class tests { [fact] public async task usingasynctask() { // arrange pubsubevent<object> pubsubevent = mock.create<pubsubevent<object>>(); // act await task.run(() => { pubsubevent.publish(new object()); }); // assert mock.assert(() => pubsubevent.publish(null), args.ignore(), occurs.once()); } }
add following nuget packages.config:
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="justmock" version="2015.1.224.3" targetframework="net45" /> <package id="nito.asyncex" version="3.0.0" targetframework="net45" /> <package id="prism.pubsubevents" version="1.1.2" targetframework="net45" /> <package id="xunit" version="2.0.0" targetframework="net45" /> <package id="xunit.abstractions" version="2.0.0" targetframework="net45" /> <package id="xunit.assert" version="2.0.0" targetframework="net45" /> <package id="xunit.core" version="2.0.0" targetframework="net45" /> <package id="xunit.extensibility.core" version="2.0.0" targetframework="net45" /> <package id="xunit.runner.visualstudio" version="2.0.0" targetframework="net45" /> </packages>
the test fails in xunit indicating expecting 1 call has received 0 calls. if remove task , run synchronously test passes.
Comments
Post a Comment