Testing that an event has an assigned EventHandler

I want to verify that the class has an EventHandler assigned to the event. I mainly use my IoC container to connect EventHandlers for me, and I want to check that they are correctly assigned. So really, I'm testing the IoC configuration.

[Test]
public void create_person_event_handler_is_hooked_up_by_windsor()
{
    IChangePersonService changePersonService = IoC.Resolve<IChangePersonService>();

    // check that changePersonService.PersonCreated has a handler attached
}

I am not sure how to verify that changePersonService.PersonCreated has everything related to it.

Any ideas?

Thanks.

+5
source share
4 answers

Without asking what you are pretending to be, the only way to test and list registered events is to register them in your own collection.

See this example:

public class MyChangePersonService : IChangePersonService
{
    private IList<EventHandler> handlers;

    private EventHandler _personEvent;

    public event EventHandler PersonCreated
    {
        add
        {
            _personEvent += value;
            handlers.Add(value);
        }

        remove
        {
            _personEvent -= value;
            handlers.Remove(value);
        }
    }

    public IList<EventHandler> PersonEventHandlers { get { return handlers; } }

    public MyChangePersonService()
    {
        handlers = new List<EventHandler>();
    }

    public void FirePersonEvent()
    {
        _personEvent(this, null);
    }
}

prop PersonEventHandlers.

- ?

+2

, unit test Castle Windsor. , , , , . , (, ).

+1

, , , . , , .

, , API IChangePersonService, .

0
source

This is not unit testing, it is an integration test. And I do not test Castle Windsor, but my configuration. I am testing that all my decoupled classes are connecting as I assume.

Ultimately, I want to verify that my configuration correctly hooks events up as I want, but I'm not sure C # will allow me without changing my API, as Yuval says.

0
source

All Articles