xUnit IClassFixture 在每次测试中都不是 运行
xUnit IClassFixture is not run at every test
根据 IClassFixture https://xunit.net/docs/shared-context 的文档,它说
"Just before the first tests in MyDatabaseTests is run, xUnit.net will create an instance of DatabaseFixture. For each test, it will create a new instance of MyDatabaseTests, and pass the shared instance of DatabaseFixture to the constructor."
我盯着 "For each test" 部分看。因为,它只调用一次继承的夹具,至少在我的代码中是这样。
public class TransportSequenceResourceAggregateServiceTests : IClassFixture<TransportSequenceResourceAggregateServiceFixture>
{
private TransportSequenceResourceAggregateServiceFixture fixture;
public TransportSequenceResourceAggregateServiceTests(TransportSequenceResourceAggregateServiceFixture fixture)
{
this.fixture = fixture;
}
[Fact]
public void If_No_Aggregate_Exist_Return_Null()
{
var sut = fixture.transportSequenceResourceAggregateService;
var aggrate = sut.Get("some_new_resource");
Assert.True(aggrate == null);
}
[Fact]
public void If_Resource_Does_Not_Exists_Create_New_Aggreagate()
{
var sut = fixture.transportSequenceResourceAggregateService;
var aggrate = sut.Create("some_new_resource");
Assert.True(aggrate.ResourcePosition == null);
Assert.True(aggrate.TransportSequences == null);
}
但也许我读错了?
难道不应该在每次测试时都创建一个新的夹具 运行 吗?
谢谢
注意 MyDatabaseTests
是你的测试 class 而 DatabaseFixture
是 class 你想要实例化一次并传递给每个测试的实例
Just before the first tests in MyDatabaseTests is run, xUnit.net will
create an instance of DatabaseFixture.
然后
For each test, it will create a new instance of MyDatabaseTests, and
pass the shared instance of DatabaseFixture to the constructor
将是:为每个测试 xUnit 创建新的测试实例 class 并传递已创建的 fixture
实例
另一种方法
MyDatabaseTests => TransportSequenceResourceAggregateServiceTests
DatabaseFixture => TransportSequenceResourceAggregateServiceFixture
因此 fixture 只会创建一次。
根据 IClassFixture https://xunit.net/docs/shared-context 的文档,它说
"Just before the first tests in MyDatabaseTests is run, xUnit.net will create an instance of DatabaseFixture. For each test, it will create a new instance of MyDatabaseTests, and pass the shared instance of DatabaseFixture to the constructor."
我盯着 "For each test" 部分看。因为,它只调用一次继承的夹具,至少在我的代码中是这样。
public class TransportSequenceResourceAggregateServiceTests : IClassFixture<TransportSequenceResourceAggregateServiceFixture>
{
private TransportSequenceResourceAggregateServiceFixture fixture;
public TransportSequenceResourceAggregateServiceTests(TransportSequenceResourceAggregateServiceFixture fixture)
{
this.fixture = fixture;
}
[Fact]
public void If_No_Aggregate_Exist_Return_Null()
{
var sut = fixture.transportSequenceResourceAggregateService;
var aggrate = sut.Get("some_new_resource");
Assert.True(aggrate == null);
}
[Fact]
public void If_Resource_Does_Not_Exists_Create_New_Aggreagate()
{
var sut = fixture.transportSequenceResourceAggregateService;
var aggrate = sut.Create("some_new_resource");
Assert.True(aggrate.ResourcePosition == null);
Assert.True(aggrate.TransportSequences == null);
}
但也许我读错了?
难道不应该在每次测试时都创建一个新的夹具 运行 吗?
谢谢
注意 MyDatabaseTests
是你的测试 class 而 DatabaseFixture
是 class 你想要实例化一次并传递给每个测试的实例
Just before the first tests in MyDatabaseTests is run, xUnit.net will create an instance of DatabaseFixture.
然后
For each test, it will create a new instance of MyDatabaseTests, and pass the shared instance of DatabaseFixture to the constructor
将是:为每个测试 xUnit 创建新的测试实例 class 并传递已创建的 fixture
实例另一种方法
MyDatabaseTests => TransportSequenceResourceAggregateServiceTests
DatabaseFixture => TransportSequenceResourceAggregateServiceFixture
因此 fixture 只会创建一次。