Last active
August 29, 2015 14:00
-
-
Save JakeGinnivan/11393228 to your computer and use it in GitHub Desktop.
Refactoring data driven nUnit test to BDDfy example test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Start with a nUnit Test case | |
[TestCase(Foo.Bar, Foo.Baz, Foo.Qux)] | |
[TestCase(Foo.Baz, Foo.Qux, Foo.Bar)] | |
public void MyTest(Foo givenArg, Foo whenArg, Foo thenArg) | |
{ | |
this.Given(_ => GivenSomething(givenArg)) | |
.When(_ => WhenSomething(whenArg)) | |
.Then(_ => ThenSomething(thenArg)) | |
.BDDfy(); | |
} | |
// Add example table | |
[TestCase(Foo.Bar, Foo.Baz, Foo.Qux)] | |
[TestCase(Foo.Baz, Foo.Qux, Foo.Bar)] | |
public void MyTest(Foo givenArg, Foo whenArg, Foo thenArg) | |
{ | |
var examples = new ExampleTable("Given arg", "When arg", "Then arg") | |
{ | |
}; | |
this.Given(_ => GivenSomething(givenArg)) | |
.When(_ => WhenSomething(whenArg)) | |
.Then(_ => ThenSomething(thenArg)) | |
.WithExamples(examples) | |
.BDDfy(); | |
} | |
// Introduce field on `givenArg` | |
Foo givenArg; | |
[TestCase(Foo.Bar, Foo.Baz, Foo.Qux)] | |
[TestCase(Foo.Baz, Foo.Qux, Foo.Bar)] | |
public void MyTest(Foo givenArg, Foo whenArg, Foo thenArg) | |
{ | |
this.givenArg = givenArg; | |
var examples = new ExampleTable("Given arg", "When arg", "Then arg") | |
{ | |
}; | |
this.Given(_ => GivenSomething(givenArg)) | |
.When(_ => WhenSomething(whenArg)) | |
.Then(_ => ThenSomething(thenArg)) | |
.WithExamples(examples) | |
.BDDfy(); | |
} | |
// Delete givenArg from test parameters | |
Foo givenArg; | |
[TestCase(Foo.Baz, Foo.Qux)] | |
[TestCase(Foo.Qux, Foo.Bar)] | |
public void MyTest(Foo whenArg, Foo thenArg) | |
{ | |
var examples = new ExampleTable("Given arg", "When arg", "Then arg") | |
{ | |
{ Foo.Bar }, | |
{ Foo.Baz } | |
}; | |
this.Given(_ => GivenSomething(givenArg)) | |
.When(_ => WhenSomething(whenArg)) | |
.Then(_ => ThenSomething(thenArg)) | |
.WithExamples(examples) | |
.BDDfy(); | |
} | |
// Repeat for whenArg | |
Foo givenArg; | |
Foo whenArg; | |
[TestCase(Foo.Qux)] | |
[TestCase(Foo.Bar)] | |
public void MyTest(Foo thenArg) | |
{ | |
var examples = new ExampleTable("Given arg", "When arg", "Then arg") | |
{ | |
{ Foo.Bar, Foo.Baz }, | |
{ Foo.Baz, Foo.Qux } | |
}; | |
this.Given(_ => GivenSomething(givenArg)) | |
.When(_ => WhenSomething(whenArg)) | |
.Then(_ => ThenSomething(thenArg)) | |
.WithExamples(examples) | |
.BDDfy(); | |
} | |
// Repeat for thenArg | |
Foo givenArg; | |
Foo whenArg; | |
Foo thenArg; | |
[TestCase()] | |
[TestCase()] | |
public void MyTest() | |
{ | |
var examples = new ExampleTable("Given arg", "When arg", "Then arg") | |
{ | |
{ Foo.Bar, Foo.Baz, Foo.Qux }, | |
{ Foo.Baz, Foo.Qux, Foo.Bar } | |
}; | |
this.Given(_ => GivenSomething(givenArg)) | |
.When(_ => WhenSomething(whenArg)) | |
.Then(_ => ThenSomething(thenArg)) | |
.WithExamples(examples) | |
.BDDfy(); | |
} | |
// Replace TestCase attributes with [Test] | |
Foo givenArg; | |
Foo whenArg; | |
Foo thenArg; | |
[Test] | |
public void MyTest() | |
{ | |
var examples = new ExampleTable("Given arg", "When arg", "Then arg") | |
{ | |
{ Foo.Bar, Foo.Baz, Foo.Qux }, | |
{ Foo.Baz, Foo.Qux, Foo.Bar } | |
}; | |
this.Given(_ => GivenSomething(givenArg)) | |
.When(_ => WhenSomething(whenArg)) | |
.Then(_ => ThenSomething(thenArg)) | |
.WithExamples(examples) | |
.BDDfy(); | |
} | |
// Add pragma to ignore warning that private fields are not assigned | |
#pragma warning disable 0649 | |
Foo givenArg; | |
Foo whenArg; | |
Foo thenArg; | |
[Test] | |
public void MyTest() | |
{ | |
var examples = new ExampleTable("Given arg", "When arg", "Then arg") | |
{ | |
{ Foo.Bar, Foo.Baz, Foo.Qux }, | |
{ Foo.Baz, Foo.Qux, Foo.Bar } | |
}; | |
this.Given(_ => GivenSomething(givenArg)) | |
.When(_ => WhenSomething(whenArg)) | |
.Then(_ => ThenSomething(thenArg)) | |
.WithExamples(examples) | |
.BDDfy(); | |
} | |
// Done, you now have a BDDfy example test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment