Tags: , , | Posted by AlexanderZeitler on 10/31/2009 12:47 AM | Comments (4)

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.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Comments are closed