Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Assert that an Event Has been Subscribed To with FakeItEasy?

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!

like image 994
Shay Friedman Avatar asked Dec 21 '11 15:12

Shay Friedman


1 Answers

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.

like image 165
Patrik Hägne Avatar answered Sep 28 '22 04:09

Patrik Hägne