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

前端 未结 7 2303
小蘑菇
小蘑菇 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条回答
  •  清歌不尽
    2020-11-29 16:53

    This is very old and probably wont even be read but with some cool new .net features I have created an INPC Tracer class that allows that:

    [Test]
    public void Test_Notify_Property_Changed_Fired()
    {
        var p = new Project();
    
        var tracer = new INCPTracer();
    
        // One event
        tracer.With(p).CheckThat(() => p.Active = true).RaisedEvent(() => p.Active);
    
        // Two events in exact order
        tracer.With(p).CheckThat(() => p.Path = "test").RaisedEvent(() => p.Path).RaisedEvent(() => p.Active);
    }
    

    See gist: https://gist.github.com/Seikilos/6224204

提交回复
热议问题