在xunit.net中,我试图在ConfigFixture
和LogFixture
中使用相同的DbFixture
,但无济于事。
为什么我要这样做是因为我想在其他灯具中使用相同的配置夹具,因此我可以将类关注分开,即将配置夹具作为需要的构建块引入。
示例:
public class Test : IClassFixture<DbFixture>
{
public Test(DbFixture db)
{
}
[Fact]
public void MyTest()
{
Assert.True(true);
}
}
public class DbFixture : IClassFixture<LogFixture>, IClassFixture<ConfigFixture>
{
public DbFixture(LogFixture log, ConfigFixture conf)
{
SetupDbConnection(conf.ConnectionString);
SetupEfLogging(log);
}
}
public class LogFixture : IClassFixture<ConfigFixture>
{
public LogFixture(ConfigFixture conf)
{
if (conf.IsLoggingEnabled)
SetupXunitLogging();
}
}
public class ConfigFixture
{
public string ConnectionString => Configuration.GetConnectionString("MyDb");
public bool IsLoggingEnabled => Configuration.GetValue<bool>("EnableLogging");
}
运行此代码会引发此错误
System.AggregateException:发生了一个或多个错误。 (类夹具类型'MyTests.TestFixtureA'有一个或多个未解析的构造函数参数:TestFixtureB b,TestFixtureC c)(以下构造函数参数没有匹配的夹具数据:TestFixtureA a) ----类夹具类型'MyTests.TestFixtureA'有一个或多个未解析的构造函数参数:TestFixtureB b,TestFixtureC c ----以下构造函数参数没有匹配的fixture数据:TestFixtureA a
-----内部堆栈跟踪#1(Xunit.Sdk.TestClassException)-----
-----内部堆栈跟踪#2(Xunit.Sdk.TestClassException)-----
我应该如何设置它才能使其正常工作?