这是关于specflow中上下文注入的问题。我的实体上课:

public class OrderInfo
{
    public string Status { get; set; }
    public string OrderName { get; set; }
}


在另一个类中,我使用此实体创建新订单:

[Binding]
public class OrderSteps
{
    private NgWebDriver driver;
    private OrderInfo orderInfo = new OrderInfo();

    public OrderSteps(NgWebDriver driver,OrderInfo orderInfo)
    {
        this.orderInfo = orderInfo;
        this.driver = driver;
    }

    [When(@"I am creating new order")]
    public void WhenIAmCreatingNewOrder(Table table)
    {
        var orderListPage = new OrdersListPage(driver);
        orderInfo = orderListPage.CreateNewOrder(table.CreateInstance<OrderInfo>());
    }
}


在此类中,我的方法返回一些存储在“ orderInfo”对象中的数据。一切正常,直到我需要在另一个类中使用该对象的数据为止。我正在尝试获取对象数据,但是里面是空的。

    [Binding]
public class BuyerPortalSteps
{
    private NgWebDriver driver;
    private OrderInfo orderInfo;

    public BuyerPortalSteps(NgWebDriver driver, OrderInfo orderInfo)
    {
        this.driver = driver;
        this.orderInfo = orderInfo;
    }
}


所以问题是:如何使用已填充第一类数据的对象来使用第二类数据

最佳答案

好像我找到了解决方案。我为我的OrderInfo做了一个子子类:

public class OrderInfoContext
{
    public OrderInfo OrderInfo { get; set; }
}


在我的Binding类中有一些更改:

public class OrderSteps
{
    private NgWebDriver driver;
    private List<FilterInfo> filterData = new List<FilterInfo>();
    private OrderInfoContext orderInfoContext;
    private OrderInfo orderInfo;

    public OrderSteps(NgWebDriver driver, OrderInfoContext orderInfoContext)
    {
        this.driver = driver;
        this.orderInfoContext = orderInfoContext;
    }

    [When(@"I am creating new order")]
    public void WhenIAmCreatingNewOrder(Table table)
    {
        var orderListPage = new OrdersListPage(driver);
        orderInfo = orderListPage.CreateNewOrder(table.CreateInstance<OrderInfo>());
        orderInfoContext.OrderInfo = orderInfo;
    }
}


现在,我可以在其他课程中使用存储的信息了:

    [Binding]
public class BuyerPortalSteps
{
    private NgWebDriver driver;
    private OrderInfoContext orderInfoContext;

    public BuyerPortalSteps(NgWebDriver driver, OrderInfoContext orderInfoContext)
    {
        this.driver = driver;
        this.orderInfoContext = orderInfoContext;
    }

    [Then(@"I am looking for order on buyer statistics grid")]
    public void ThenIAmLookingForOrderOnBuyerStatisticsGrid()
    {
        BuyerPortalDashboard buyerPortalDashboard = new BuyerPortalDashboard(driver);
        buyerPortalDashboard.CheckBuyerDashboardStatistic(orderInfoContext.OrderInfo);
    }
}


我认为原因是,如果要在类之间共享数据,则必须一次初始化该数据,而永远不要尝试再次编辑它,因为可以在阅读它之前在任何地方对其进行更改。这只是我的假设

09-04 09:22