I defined simple event in class like that
class Request
{
protected virtual void OnStateChanged()
{
StateChanged?.Invoke(this, EventArgs.Empty);
}
public event EventHandler StateChanged;
public void Approve()
{
OnStateChanged();
}
}
how do I xunit that this event were raised ?
I tried one line event handler implementation
bool eventRaised = false;
request.StateChanged += (obj, eventArgs) => eventRaised = true;
request.Approve();
Assert.True(eventRaised);
it works, but is there any better approach ? I did not figure out, how to use Assert.Raises()
Assert.Raises(request.StateChanged)
:D Also at first thought I thought I'd have to create a new method, now it's only 4 lines (but it could be one :-). – Whirlpool