Skip to content

Instantly share code, notes, and snippets.

@joebuschmann
Last active August 29, 2015 14:07
Show Gist options
  • Save joebuschmann/91a23c5066009538135b to your computer and use it in GitHub Desktop.
Save joebuschmann/91a23c5066009538135b to your computer and use it in GitHub Desktop.
Implementation of Specflow's default calculator feature using a context object to maintain state
[Binding]
public class CalculatorSteps
{
private readonly CalculatorContext _calculatorContext;
public ContextObjectSteps(CalculatorContext calculatorContext)
{
_calculatorContext = calculatorContext;
}
[Given(@"I have entered (.*) into the calculator")]
public void GivenIHaveEnteredIntoTheCalculator(int num)
{
_calculatorContext.Values.Add(num);
}
[When(@"I press add")]
public void WhenIPressAdd()
{
var calculatorService = new CalculatorService();
_calculatorContext.Result = calculatorService.Add(_calculatorContext.Values);
}
[Then(@"the result should be (.*) on the screen")]
public void ThenTheResultShouldBeOnTheScreen(int expectedResult)
{
Assert.That(_calculatorContext.Result, Is.EqualTo(expectedResult));
}
}
public class CalculatorContext
{
public CalculatorContext()
{
Values = new List<int>();
}
public List<int> Values { get; set; }
public int Result { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment