Created
March 17, 2022 08:29
-
-
Save Maesla/6800dea4ed31cdaf289371d9957ac5d5 to your computer and use it in GitHub Desktop.
Nunit helper class for testing events, custom constraint and fluent assertion included
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using NUnit.Framework.Constraints; | |
public class EventClass | |
{ | |
public int CallCount { get; private set; } | |
public void Callback() | |
{ | |
CallCount++; | |
} | |
public void Reset() | |
{ | |
CallCount = 0; | |
} | |
public override string ToString() | |
{ | |
return CallCount.ToString(); | |
} | |
} | |
public class EventReceivedConstraint : Constraint | |
{ | |
private readonly int expected; | |
public EventReceivedConstraint() : this(1) | |
{ | |
} | |
public EventReceivedConstraint(int expected) : base(expected) | |
{ | |
this.expected = expected; | |
} | |
public override string Description | |
{ | |
get { return $"receives {expected} event(s)"; } | |
} | |
public override ConstraintResult ApplyTo(object actual) | |
{ | |
int callCount = ((EventClass)actual).CallCount; | |
return new EventReceivedResult(this, actual, callCount, (int)Arguments[0]); | |
} | |
private class EventReceivedResult : ConstraintResult | |
{ | |
private readonly int actualReceived; | |
private readonly int expetedReceived; | |
public EventReceivedResult(IConstraint constraint, object actualValue, int actualReceived, int expectedReceived) : base(constraint, actualValue, actualReceived == expectedReceived) | |
{ | |
this.actualReceived = actualReceived; | |
this.expetedReceived = expectedReceived; | |
} | |
public override void WriteMessageTo(MessageWriter writer) | |
{ | |
if (Status == ConstraintStatus.Success) | |
{ | |
writer.WriteMessageLine($"The event was received a {0} time(s)", expetedReceived); | |
} | |
if (Status == ConstraintStatus.Failure) | |
{ | |
writer.WriteMessageLine("The event was received {0} time(s). Expected {1} time(s)", actualReceived, expetedReceived); | |
} | |
} | |
} | |
} | |
public static class CustomConstraintExtensions | |
{ | |
public static EventReceivedConstraint Received(this ConstraintExpression expression) | |
{ | |
var constraint = new EventReceivedConstraint(); | |
expression.Append(constraint); | |
return constraint; | |
} | |
public static EventReceivedConstraint Received(this ConstraintExpression expression, int expected) | |
{ | |
var constraint = new EventReceivedConstraint(expected); | |
expression.Append(constraint); | |
return constraint; | |
} | |
} | |
public class Is : NUnit.Framework.Is | |
{ | |
public static EventReceivedConstraint Received(int expected) | |
{ | |
return new EventReceivedConstraint(expected); | |
} | |
public static EventReceivedConstraint Received() | |
{ | |
return new EventReceivedConstraint(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use it: