The reason why lambda function is not being executed in the test is the following:
There are 2 lambdas:
The one declared in test method:
Action action = osgController.AddWindow;
The other one declared in the class under test:
() => _osgController.AddWindow()
They do exactly the same but they are different.
That is why setup for dispatcher.Expect(d => d.BeginInvoke(action))
doesn't match to the actual action passed to BeginInvoke()
. As a result WhenCalled()
argument is not being triggered.
The one of solutions is to setup the stub for dispatcher.BeginInvoke()
which accepts any action and just executes it:
dispatcher
.Stub(d => d.BeginInvoke(Arg<Action>.Is.Anything))
.WhenCalled(opt => ((Action)(opt.Arguments[0]))());
Also, Do()
handler can be used instead of WhenCalled()
here. Then arguments cast isn't required:
dispatcher
.Stub(d => d.BeginInvoke(Arg<Action>.Is.Anything))
.Do((Action<Action>)(action => action()));
Hope that helps.