Mit den xUnit.net BDD Extensions kann man natürlich auch auf erwartete Exceptions prüfen – wie das geht, zeigt der folgende Code:
[Concern(typeof (IMyClass))]
public class when_my_instance_is_created_with_empty_parameter : StaticContextSpecification {
private IMyParameter _myParameter;
private Action _theCreateInstanceAction;
protected override void EstablishContext() {
_myParameter = null;
}
protected override void Because() {
_theCreateInstanceAction = new Action(() => MyClass.CreateInstance(_myParameter));
}
[Observation]
public void should_throw_argumentnullexception() {
_theCreateInstanceAction.ShouldThrowAn<ArgumentNullException>();
}
}
Früher wurde _theCreateInstanceAction übrigens mittels
_theCreateInstanceAction = The.Action(() => MyClass.CreateInstance(_myParameter));
erzeugt, das ist allerdings inzwischen obsolet.
Update: Bitte beachtet auch den Kommentar von Björn.