Last active
August 29, 2015 14:07
-
-
Save joebuschmann/e3167213875f7e8dbec5 to your computer and use it in GitHub Desktop.
Implementation of Specflow's default calculator feature using ScenarioContext to maintain state
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
[Binding] | |
public class CalculatorSteps | |
{ | |
private const string CalculatorValues = "calculatorValues"; | |
private const string CalculationResult = "calculationResult"; | |
[Given(@"I have entered (.*) into the calculator")] | |
public void GivenIHaveEnteredIntoTheCalculator(int num) | |
{ | |
List<int> values; | |
if (!ScenarioContext.Current.ContainsKey(CalculatorValues)) | |
{ | |
values = new List<int>(); | |
ScenarioContext.Current.Add(CalculatorValues, values); | |
} | |
else | |
{ | |
values = ScenarioContext.Current.Get<List<int>>(CalculatorValues); | |
} | |
values.Add(num); | |
} | |
[When(@"I press add")] | |
public void WhenIPressAdd() | |
{ | |
var values = ScenarioContext.Current.Get<List<int>>(CalculatorValues); | |
var calculatorService = new CalculatorService(); | |
int result = calculatorService.Add(values); | |
ScenarioContext.Current.Add(CalculationResult, result); | |
} | |
[Then(@"the result should be (.*) on the screen")] | |
public void ThenTheResultShouldBeOnTheScreen(int expectedResult) | |
{ | |
var calculationResult = ScenarioContext.Current.Get<int>(CalculationResult); | |
Assert.That(calculationResult, Is.EqualTo(expectedResult)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment