Skip to content

Instantly share code, notes, and snippets.

@joebuschmann
Last active August 29, 2015 14:07
Show Gist options
  • Save joebuschmann/e3167213875f7e8dbec5 to your computer and use it in GitHub Desktop.
Save joebuschmann/e3167213875f7e8dbec5 to your computer and use it in GitHub Desktop.
Implementation of Specflow's default calculator feature using ScenarioContext to maintain state
[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