Skip to content

Commit

Permalink
Merge pull request nspec#13 from jgill13/ExpectExceptionUpdate
Browse files Browse the repository at this point in the history
Updated "expect" to test the exception message
  • Loading branch information
mattflo committed Aug 3, 2011
2 parents b27eff3 + 077b00e commit 449fcb5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
15 changes: 15 additions & 0 deletions NSpec/nspec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ public virtual Action act
/// <para>it["should throw exception"] = expect&lt;InvalidOperationException&gt;(() => SomeMethodThatThrowsException());</para>
/// </summary>
public virtual Action expect<T>(Action action) where T : Exception
{
return expect<T>( null, action );
}

/// <summary>
/// Set up an expectation for a particular exception type to be thrown with an expected message.
/// <para>For Example:</para>
/// <para>it["should throw exception with message Error"] = expect&lt;InvalidOperationException&gt;("Error", () => SomeMethodThatThrowsException());</para>
/// </summary>
public virtual Action expect<T>(string expectedMessage, Action action) where T : Exception
{
return () =>
{
Expand All @@ -130,6 +140,11 @@ public virtual Action expect<T>(Action action) where T : Exception
{
throw new ExceptionNotThrown("Exception of type " + typeof(T).Name + " was not thrown.");
}

if( expectedMessage != null && expectedMessage != ex.Message )
{
throw new ExceptionNotThrown( String.Format( "Expected message: \"{0}\" But was: \"{1}\"", expectedMessage, ex.Message ) );
}
}
};
}
Expand Down
19 changes: 18 additions & 1 deletion NSpecSpecs/describe_RunningSpecs/describe_expected_exception.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ void method_level_context()

it["should throw exception"] = expect<InvalidOperationException>(() => { throw new InvalidOperationException(); });

it["should throw exception with error message Testing"] = expect<InvalidOperationException>( "Testing", () => { throw new InvalidOperationException("Testing"); });

it["should fail if no exception thrown"] = expect<InvalidOperationException>(() => { });

it["should fail if wrong exception thrown"] = expect<InvalidOperationException>(() => { throw new ArgumentException(); });

it["should fail if wrong error message is returned"] = expect<InvalidOperationException>("Testing", () => { throw new InvalidOperationException("Blah"); });
}
}

Expand All @@ -33,7 +37,7 @@ public void setup()
[Test]
public void should_be_two_failures()
{
classContext.Failures().Count().should_be(2);
classContext.Failures().Count().should_be(3);
}

[Test]
Expand All @@ -42,6 +46,12 @@ public void given_exception_is_thrown_should_not_fail()
TheExample("should throw exception").Exception.should_be(null);
}

[Test]
public void given_exception_is_thrown_with_expected_message_should_not_fail()
{
TheExample("should throw exception with error message Testing").Exception.should_be(null);
}

[Test]
public void given_exception_not_thrown_should_fail()
{
Expand All @@ -55,6 +65,13 @@ public void given_wrong_exception_should_fail()
TheExample("should fail if wrong exception thrown").Exception.Message.should_be("Exception of type InvalidOperationException was not thrown.");
}

[Test]
public void given_wrong_error_message_should_fail()
{
TheExample("should fail if wrong error message is returned").Exception.GetType().should_be(typeof(ExceptionNotThrown));
TheExample("should fail if wrong error message is returned").Exception.Message.should_be("Expected message: \"Testing\" But was: \"Blah\"");
}

private Example TheExample(string name)
{
return classContext.Contexts.First().AllExamples().Single(s => s.Spec == name);
Expand Down

0 comments on commit 449fcb5

Please sign in to comment.