从步骤定义中导航到Specflow步骤

时间:2016-08-02 14:54:44

标签: specflow

所以我有几个测试,我已经从步骤中重复使用了步骤。

但我现在在维修方面遇到了噩梦,因为我无法轻易地在两个步骤之间导航。

以下是一个例子:

    [Given(@"I have an order")]
    public void GivenIHaveAnOrder()
    {
        Given("an open store");
        Given("I am an existing customer");
        Given("I am on homepage");
        When("I search for a component");
        When("I add the component to my basket");
    }

如何导航到其中一个内部步骤?

如果我想导航到"何时("我搜索组件");"第一步我不能。

如果我在功能文件中,我可以直接右键单击该步骤"转到定义"但我不能在这里这样做。有没有人有解决方案?

1 个答案:

答案 0 :(得分:1)

我假设您使用Given / When-函数调用这些步骤,因为它们位于不同的绑定类中。我是对的吗?

有一种更好的方法,比使用这个功能。

您是否看过驱动程序概念和上下文注入? 看看这里:http://www.specflow.org/documentation/Context-Injection/

只需将您的步骤逻辑提取到驱动程序类,然后在不同的步骤类中从中获取实例:

class Driver 
{
    public void AnOpenStore()
    {
        ...
    } 
}

[Binding]
public class StepClass1
{
     private Driver _driver;   

     public StepClass1(Driver driver)
     {
          _driver = driver;
     }

     [Given(@"I have an order")]
     public void IHaveAnOrder()
     {
          _driver.AnOpenStore();
     }
}

[Binding]
public class StepClass2
{
     private Driver _driver;   

     public StepClass2(Driver driver)
     {
          _driver = driver;
     }

     [Given(@"an open store")]
     public void AnOpenStore()
     {
          _driver.AnOpenStore();
     }
}

当您安排这样的步骤实施时,重复使用其他步骤会更容易。

相关问题