Skip to content

Commit

Permalink
fix: Throw exceptions which occur in the Establish delegate.
Browse files Browse the repository at this point in the history
In the event that an exception is thrown from both the Establish and the Because delegate, the exception thrown by Establish was not being thrown when executing the It delegates.  This corrects the issue by skipping the execution of the Because delegate when an exception is thrown by the Establish delegate.
  • Loading branch information
Michael Shattuck authored and Derek Greer committed Jul 15, 2017
1 parent efadc24 commit a4e2166
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
50 changes: 49 additions & 1 deletion src/NUnit.Specifications.Specs/ContextSpecificationSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using NUnit.Framework;
using NUnit.Specifications.Categories;

Expand All @@ -24,5 +25,52 @@ public class when_executing_a_specification : ContextSpecification

It should_execute_the_it_delegate_successfully = () => Assert.IsTrue(true);
}

[TestFixture]
[Subcutaneous]
[Subject("Context Specification")]
public class when_executing_a_specification_and_an_exception_is_thrown_in_the_establish : ContextSpecification
{
static SubjectUnderTest _subjectUnderTest;
static Exception _exception;

Establish context = () => _subjectUnderTest = new SubjectUnderTest();

Because of = () => _exception = Catch.Exception(() => _subjectUnderTest.TestFixtureSetUp());

It should_bubble_the_exception_from_the_establish = () => Assert.AreEqual("context", _subjectUnderTest.CaughtException.Message);

class SubjectUnderTest : ContextSpecification
{
public Exception CaughtException => Exception.InnerException;
Establish context = () => throw new Exception("context");
Because of = () => throw new Exception("because");
}
}

[TestFixture]
[Subcutaneous]
[Subject("Context Specification")]
public class when_executing_a_specification_and_an_exception_is_thrown_in_the_because: ContextSpecification
{
static SubjectUnderTest _subjectUnderTest;
static Exception _exception;
static bool _establishHasRun;

Establish context = () => _subjectUnderTest = new SubjectUnderTest();

Because of = () => _exception = Catch.Exception(() => _subjectUnderTest.TestFixtureSetUp());

It should_call_the_establish = () => Assert.IsTrue(_establishHasRun);

It should_bubble_the_exception_from_the_because = () => Assert.AreEqual("because", _subjectUnderTest.CaughtException.Message);

class SubjectUnderTest : ContextSpecification
{
public Exception CaughtException => Exception.InnerException;
Establish context = () => _establishHasRun = true;
Because of = () => throw new Exception("because");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageReference Include="Moq" Version="4.7.10" />
<PackageReference Include="NUnit" Version="3.6.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0-ci-00452-pr-313" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0-ci-00459-pr-313" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions src/NUnit.Specifications/ContextSpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ void InvokeEstablish()

void InvokeBecause()
{
if (Exception != null)
return;

var t = GetType();

var fieldInfos = t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic |
Expand Down

0 comments on commit a4e2166

Please sign in to comment.