Using FakeItEasy to have a faked method call raise an event?
Asked Answered
C

1

5

I'm trying to do something along the lines of:

A.CallTo(() => fakeTimer.Start()).Invokes(() => 
    fakeTimer.Elapsed += Raise.With<ElapsedEventArgs>(ElapsedEventArgs.Empty).Now);

The fakeTimer is a fake of ITimer, a wrapper interface per this answer.

Obviously this doesn't work, since I cannot do an assignment inside an Experssion Tree.

What I am actually tying to achieve is simulating raising timer events when the Start method was called. This way I can assert that a call to Start indeed happened.

Any (alternative) ideas?

Edit I'm an idiot and the fault is my own! I accidentally added an extra A.CallTo, where I shouldn't have. Instead of deleting this question, I'll keep it to award Patrik Hägne his rightful reputation :)

Concavoconvex answered 24/7, 2012 at 14:25 Comment(4)
Try wrapping the expression with braces: A.CallTo(() => { ... }).Silage
@Silage it doesn't work, because a Lambda expression with a statement body ({...}) cannot be converted to an Expression Tree...Concavoconvex
Ah, it takes a Expression<Action> (source link). You might have to explicitly cast it then, since Action and Expression<Action> are technically ambiguous: (Expression<Action>)(() => ... ).Silage
I think you should've left the question in its original state to help googlers. I googled "fake it easy event was raised" and got here, and was confused because the same code snippet is in the answer and question.Sosanna
S
9

I'm not really sure what you're trying to do, is this it???

A.CallTo(() => fakeTimer.Start()).Invokes(() => 
    fakeTimer.Elapsed += Raise.With<ElapsedEventArgs>(ElapsedEventArgs.Empty).Now);
Sorely answered 25/7, 2012 at 9:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.