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:

  1. you appear checking if method call happened before run execute. since execute raises event triggers method call, call not yet have been made.
  2. 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(); 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -