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 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.
The maximum you can do is to check if the event equals to null, it will return whether something is subscribed to it or not.
Otherwise, you can't know which or how many handlers are subscribed to an event.
© 2022 - 2024 — McMap. All rights reserved.