I have a class that raises an event with an error message.
In some of my tests I am subscribing to the event and asserting that the error message is not empty.
[Test]
public MyMethod_DoBad_ErrorMessageNotEmpty()
{
var logic = new MyClass();
string ErrorMessage = String.Empty;
logic.DisplayError += delegate(string s)
{
ErrorMessage = s;
};
logic.DoItBadly();
Assert.IsFalse(String.IsNullOrWhiteSpace(ErrorMessage));
}
//MyClass
public void DoItBadly()
{
//do something naughty but not final
DisplayError("Naughty");
//some other problem arises
if (1==1)
DisplayError("Something else naughty");
}
However I am starting to find in edge case testing that my new tests that should fail, pass because it has raised an error event previously in the code before it has got to where I want it to.
Therefore should I be asserting that the error message contains a specified string?
I think you should test both this cases in different tests:
Or, if you need to check both errors where raised:
UPDATE:
Considering event-based nature of your notifications, you can catch them all and then search for some concrete error: