Created
June 29, 2014 14:17
-
-
Save MikeMKH/6f76a660f2027611719d to your computer and use it in GitHub Desktop.
Proving FizzBuzz traits in C# using SpecFlow with NUnit.
This file contains hidden or 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
namespace FizzBuzz | |
{ | |
public class FizzBuzzer | |
{ | |
public string Translate(int value) | |
{ | |
var result = string.Empty; | |
if (value % 3 == 0) result += "Fizz"; | |
if (value % 5 == 0) result += "Buzz"; | |
return string.IsNullOrEmpty(result) ? value.ToString() : result; | |
} | |
} | |
} |
This file contains hidden or 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
Feature: FizzBuzzerSpec | |
I want to be able to translate numbers to their FizzBuzz value | |
In order to be able to communicate with the FizzBuzzons | |
As a Starfleet Captain | |
Scenario: 2 must translate to the string 2 | |
Given a FizzBuzzer | |
And the value of '2' | |
When translate is invoked | |
Then the result should be '2' | |
Scenario: 3 must translate to the string Fizz | |
Given a FizzBuzzer | |
And the value of '3' | |
When translate is invoked | |
Then the result should be 'Fizz' | |
Scenario: 5 must translate to the string Buzz | |
Given a FizzBuzzer | |
And the value of '5' | |
When translate is invoked | |
Then the result should be 'Buzz' | |
Scenario: 15 must translate to the string FizzBuzz | |
Given a FizzBuzzer | |
And the value of '15' | |
When translate is invoked | |
Then the result should be 'FizzBuzz' | |
Scenario Outline: FizzBuzz translation | |
Given a FizzBuzzer | |
And the value of '<value>' | |
When translate is invoked | |
Then the result should be '<expected value>' | |
Examples: | |
| value | expected value | | |
| 2 | 2 | | |
| 4 | 4 | | |
| 3 | Fizz | | |
| 9 | Fizz | | |
| 5 | Buzz | | |
| 25 | Buzz | | |
| 15 | FizzBuzz | | |
| 30 | FizzBuzz | |
This file contains hidden or 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
using FizzBuzz; | |
using TechTalk.SpecFlow; | |
using NUnit.Framework; | |
namespace FizzBuzzSpec | |
{ | |
[Binding] | |
public class FizzBuzzerSpecSteps | |
{ | |
[Given(@"a FizzBuzzer")] | |
public void GivenAFizzBuzzer() | |
{ | |
ScenarioContext.Current.Add("FizzBuzzer", new FizzBuzzer()); | |
} | |
[Given(@"the value of '(.*)'")] | |
public void GivenTheValueOf(int value) | |
{ | |
ScenarioContext.Current.Add("value", value); | |
} | |
[When(@"translate is invoked")] | |
public void WhenTranslateIsInvoked() | |
{ | |
var fizzbuzzer = ScenarioContext.Current.Get<FizzBuzzer>("FizzBuzzer"); | |
var value = ScenarioContext.Current.Get<int>("value"); | |
ScenarioContext.Current.Add("actual", fizzbuzzer.Translate(value)); | |
} | |
[Then(@"the result should be '(.*)'")] | |
public void ThenTheResultShouldBe(string expected) | |
{ | |
var actual = ScenarioContext.Current.Get<string>("actual"); | |
Assert.That(expected, Is.EqualTo(actual)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See my blog post which goes with this gist: http://comp-phil.blogspot.com/2014/06/proving-traits.html