Skip to content

Instantly share code, notes, and snippets.

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