Unit testing that events are raised in C# (in order)

前端 未结 7 2291
小蘑菇
小蘑菇 2020-11-29 16:02

I have some code that raises PropertyChanged events and I would like to be able to unit test that the events are being raised correctly.

The code that i

7条回答
  •  Happy的楠姐
    2020-11-29 16:36

    If you're doing TDD then event testing can start to generate a lot of repetitive code. I wrote an event monitor that enables a much cleaner approach to unit test writing for these situations.

    var publisher = new PropertyChangedEventPublisher();
    
    Action test = () =>
    {
        publisher.X = 1;
        publisher.Y = 2;
    };
    
    var expectedSequence = new[] { "X", "Y" };
    
    EventMonitor.Assert(test, publisher, expectedSequence);
    

    Please see my answer to the following for more details.

    Unit testing that an event is raised in C#, using reflection

提交回复
热议问题