【发布时间】:2018-01-30 10:31:16
【问题描述】:
我有一个“测试”类,它有一个 IEnumerable 的具体对象
public class Test
{
public IEnumerable<MyObject> MyObjects { get; }
}
public class MyObject
{
public string Id { get; }
public MyObject(
string id,
{
this.Id = id;
}
}
在调试 Specflow 功能文件使用的“Steps.cs”类时,我注意到“Id”的值发生了变化。 它们在“给定”步骤中符合预期,我将它们添加到 ScenarioContext
[Given("I have use case (.*)")]
{
var _test = _retrieveTest.GetMyObjects(useCase);
ScenarioContext.Current.Add("test", _test);
}
当我在“When”步骤中从 ScenarioContext 中读取它们时,它们被更改了
[When("I do something")]
public void WhenIDoSomething()
{
var _test = (MyProject.Entity.Test)ScenarioContext.Current["test"];
}
停止值更改的解决方案是在检索对象时使用 LINQ“ToList()”调用,如下所示:
private IEnumerable<MyObject> GetMyObjects(
string usecase,
MyProject.Entity.Test test)
{
...
return testData.Objects
.Select(Object => {
var _id = var _id = Guid.NewGuid();
return new MyProject.Entity.MyObject(
_id);
}).ToList();
}
谁能解释为什么需要在这里调用“.ToList()”,而没有它为什么“Id”的值在“Given”和“When”步骤之间的“ScenarioContext”中发生变化
【问题讨论】:
-
ScenarioContext.Current 是一个目录
。你确定你没有用另一个值覆盖一个键吗? -
是的,我确信我不会对它做任何事情,尤其是在 Given 和 When 步骤之间。我确实了解 ScenarioContext.Current 是什么以及它的用途。
标签: c# linq ienumerable specflow