I have a fake class that contains an event. My code should subscribe to that event and I want to test that. I'm using FakeItEasy with NUnit and I'm looking for a way to check that my code actually subscribes to that event.
Thanks!
I agree with the comment suggesting that you'd rather just raise the event and check that the handler you want to have subscribed has been invoked. But there is a way to check wether a handler was attached, thought not very pretty:
public interface IHaveAnEvent
{
event EventHandler MyEvent;
}
// In your test...
var fake = A.Fake<IHaveAnEvent>();
var handler = new EventHandler((s, e) => { });
fake.MyEvent += handler;
A.CallTo(fake).Where(x => x.Method.Name.Equals("add_MyEvent")).WhenArgumentsMatch(x => x.Get<EventHandler>(0).Equals(handler)).MustHaveHappened();
If you just want to check that any handler was attached you can omit the "WhenArgumentsMatch" part.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With