c# - FakeItEasy error: No calls were made to the fake object -
i have small class has small method gets invoked when event raised.
public class demoui { public demoui(testrunner runner) { runner.usermessage += oneventrunthis; } protected void oneventrunthis(object sender, usermessageeventargs e) { console.writeline(e.message); } }
now in test create object of type testrunner
, execute execute
method on it. raises event intercepted , oneventrunthis
dutifully runs printing message. fake easy reports error "no calls made fake object".
var _suttestrunner = new testrunner(); var fakedemoui = a.fake<demoui>(x => x.withargumentsforconstructor(() => new demoui(_suttestrunner))); _suttestrunner.execute(); a.callto(fakedemoui).where(x => x.method.name == "oneventrunthis").musthavehappened();
the method oneventrunthis
getting called because see output getting printed in output window. in limited understanding means call has been made fake object.
or missing something? or there other way out this?
i think see 2 problems:
- you appear checking if method call happened before run
execute
. sinceexecute
raises event triggers method call, call not yet have been made. oneventrunthis
not virtual, fakeiteasy can't override , thereby intercept call. that's why you're seeing console message—the original code being run.
you'd better off like:
public class demoui { public demoui(testrunner runner) { runner.usermessage += oneventrunthis; } protected virtual void oneventrunthis(object sender, usermessageeventargs e) { console.writeline(e.message); } } … var _suttestrunner = new testrunner(); var fakedemoui = a.fake<demoui>(x => x.withargumentsforconstructor(() => new demoui(_suttestrunner))); _suttestrunner.execute(); a.callto(fakedemoui) .where(x => x.method.name == "oneventrunthis") .musthavehappened();