我正在使用SpecFlow測試一個類MortgageCalculator。我發現我有兩種設置步驟文件的方法,一種是我有一個類級別的MortgageCalculator,另一種是我使用ScenarioContext(參見下文)。哪個更好?在SpecFlow中,我應該使用類字段還是ScenarioContext作爲我正在測試的對象?
我發現選項1要容易得多,因爲我不必繼續獲取並設置ScenarioContext。但我被教導使用ScenarioContext。
選項1:
[Binding]
public sealed class MortgageCalculatorSteps
{
private MortgageCalculator calculator;
[Given(@"I have a MortgageCalculator")]
public void GivenIHaveAMortgageCalculator()
{
calculator = new MortgageCalculator();
}
[Then(@"I can do something with the calculator")]
public void ThenICanDoSomethingWithTheCalculator()
{
calculator.DoSomething();
}
}
選項2:
[Binding]
public sealed class MortgageCalculatorSteps
{
[Given(@"I have a MortgageCalculator")]
public void GivenIHaveAMortgageCalculator()
{
var calculator = new MortgageCalculator();
ScenarioContext.Current.Set(calculator);
}
[Then(@"I can do something with the calculator")]
public void ThenICanDoSomethingWithTheCalculator()
{
var calculator = ScenarioContext.Current.Get<MortgageCalcualtor>();
calculator.DoSomething();
}
}