Skip to content

Commit bbbcc73

Browse files
authored
Add tests to prove that teardown runs for each example if any of the steps fail (#302)
1 parent 0cae778 commit bbbcc73

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using Shouldly;
2+
using System;
3+
using System.Linq;
4+
using Xunit;
5+
6+
namespace TestStack.BDDfy.Tests.Exceptions.OtherExceptions
7+
{
8+
public class WhenThenThrowsExceptionWithExamples
9+
{
10+
private InvalidOperationException _stepException = new("Ooops!");
11+
private void StepThrowsException(int input) => throw _stepException;
12+
13+
[Fact]
14+
public void EachFailedAssertionExampleRunsTeardown()
15+
{
16+
var teardowns = 0;
17+
var input = 0;
18+
var engine = this.Given("i have a test with examples")
19+
.When("i run that test")
20+
.Then(_=> StepThrowsException(input))
21+
.WithExamples(new ExampleTable("input")
22+
{
23+
{ 1 }, { 2 }, { 3 }
24+
}).TearDownWith(() => teardowns++ , "teardown")
25+
.LazyBDDfy();
26+
27+
Should.Throw<InvalidOperationException>(engine.Run);
28+
teardowns.ShouldBe(3);
29+
30+
engine.Story.Result.ShouldBe(Result.Failed);
31+
var titles = engine.Story.Scenarios.Select(s => s.Steps.Single(step => step.Result == Result.Failed).Title).ToArray();
32+
titles.ShouldAllBe(t => t.StartsWith("Then", StringComparison.OrdinalIgnoreCase));
33+
}
34+
35+
[Fact]
36+
public void EachFailedGivenExampleRunsTeardown()
37+
{
38+
var teardowns = 0;
39+
var input = 0;
40+
var engine = this.Given(_=> StepThrowsException(input))
41+
.When("i run that test")
42+
.Then("will never happen")
43+
.WithExamples(new ExampleTable("input")
44+
{
45+
{ 1 }, { 2 }, { 3 }
46+
}).TearDownWith(() => teardowns++, "teardown")
47+
.LazyBDDfy();
48+
49+
Should.Throw<InvalidOperationException>(engine.Run);
50+
teardowns.ShouldBe(3);
51+
52+
engine.Story.Result.ShouldBe(Result.Failed);
53+
var titles = engine.Story.Scenarios.Select(s=>s.Steps.Single(step=>step.Result == Result.Failed).Title).ToArray();
54+
titles.ShouldAllBe(t=> t.StartsWith("Given", StringComparison.OrdinalIgnoreCase));
55+
}
56+
57+
[Fact]
58+
public void EachFailedWhenExampleRunsTeardown()
59+
{
60+
var teardowns = 0;
61+
var input = 0;
62+
var engine = this.Given("i have a test with examples")
63+
.When(_ => StepThrowsException(input))
64+
.Then("will never happen")
65+
.WithExamples(new ExampleTable("input")
66+
{
67+
{ 1 }, { 2 }, { 3 }
68+
}).TearDownWith(() => teardowns++, "teardown")
69+
.LazyBDDfy();
70+
71+
Should.Throw<InvalidOperationException>(engine.Run);
72+
teardowns.ShouldBe(3);
73+
74+
engine.Story.Result.ShouldBe(Result.Failed);
75+
var titles = engine.Story.Scenarios.Select(s => s.Steps.Single(step => step.Result == Result.Failed).Title).ToArray();
76+
titles.ShouldAllBe(t => t.StartsWith("When", StringComparison.OrdinalIgnoreCase));
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)