获取错误以下构造函数参数没有匹配的固定数据:PostgreSqlResource资源
我正在使用Xunit进行集成测试,以下是我的测试类。
public class CodesAndGuidelinesTest : IClassFixture<SchemaCache>
{
public readonly SchemaCache schemaCache;
public CodesAndGuidelinesTest(PostgreSqlResource resource)
{
schemaCache = new SchemaCache(resource);
}
[Fact]
public async Task Create_Name_Contains_Expression()
{
IRequestExecutor requestExecutor = await schemaCache.CodesAndGuidelinesExecutor;
.......
}
}
这是此处的架构缓存类
public class SchemaCache : QueryTestBase
{
Task<IRequestExecutor> _codesAndGuidelinesExecutor;
public SchemaCache(PostgreSqlResource resource) : base(resource)
{
_codesAndGuidelinesExecutor = CreateDb(CodesAndGuidelinesMockFixture.codeStandardGuidelines);
}
public Task<IRequestExecutor> CodesAndGuidelinesExecutor
{
get { return _codesAndGuidelinesExecutor; }
}
}
codesandguidelinesmockfixture.codestandardguidelines
只是一个模拟对象,当我运行测试用例时,我会收到以下错误。
类固定装置类型'api.tests.schemacache'有一个或多个未解决的 构造函数参数:PostgreSqlresource资源, codestandardguideline [] codesandguidelines以下 构造函数参数没有匹配的固定数据: PostgreSqlresource Resource
我不确定我在哪里做错了上述代码。谁能向我指向正确的方向?
谢谢!!!
更新 : queryTestBase类
public class QueryTestBase
{
private readonly PostgreSqlResource _resource;
public QueryTestBase(PostgreSqlResource resource)
{
_resource = resource;
}
protected async Task<Func<IResolverContext, IQueryable<T>>> BuildResolverAsync<T>(T[] arrayOfEntities) where T : class
{
var databaseName = Guid.NewGuid().ToString("N");
var options = new DbContextOptionsBuilder<APIDbContext>()
.UseNpgsql(_resource.ConnectionString)
.Options;
.......
.......
return _ => set.AsQueryable();
}
protected async Task<IRequestExecutor> CreateDb<T>(T[] Entities) where T : class
{
Func<IResolverContext, IQueryable<T>> resolver = await BuildResolverAsync(Entities);
return .......
}
}
I am using xunit to do integration testing, and below is my test class.
public class CodesAndGuidelinesTest : IClassFixture<SchemaCache>
{
public readonly SchemaCache schemaCache;
public CodesAndGuidelinesTest(PostgreSqlResource resource)
{
schemaCache = new SchemaCache(resource);
}
[Fact]
public async Task Create_Name_Contains_Expression()
{
IRequestExecutor requestExecutor = await schemaCache.CodesAndGuidelinesExecutor;
.......
}
}
Here is the schema cache class
public class SchemaCache : QueryTestBase
{
Task<IRequestExecutor> _codesAndGuidelinesExecutor;
public SchemaCache(PostgreSqlResource resource) : base(resource)
{
_codesAndGuidelinesExecutor = CreateDb(CodesAndGuidelinesMockFixture.codeStandardGuidelines);
}
public Task<IRequestExecutor> CodesAndGuidelinesExecutor
{
get { return _codesAndGuidelinesExecutor; }
}
}
Here CodesAndGuidelinesMockFixture.codeStandardGuidelines
is just a mock object, and When I run the test cases, I am getting the below error.
Class fixture type 'API.Tests.SchemaCache` had one or more unresolved
constructor arguments: PostgreSqlResource resource,
CodeStandardGuideline[] codesAndGuidelines The following
constructor parameters did not have matching fixture data:
PostgreSqlResource resource
I am not sure where I am doing wrong with the above code. Could anyone point me in the right direction?
Thanks!!!
Update :
QueryTestBase class
public class QueryTestBase
{
private readonly PostgreSqlResource _resource;
public QueryTestBase(PostgreSqlResource resource)
{
_resource = resource;
}
protected async Task<Func<IResolverContext, IQueryable<T>>> BuildResolverAsync<T>(T[] arrayOfEntities) where T : class
{
var databaseName = Guid.NewGuid().ToString("N");
var options = new DbContextOptionsBuilder<APIDbContext>()
.UseNpgsql(_resource.ConnectionString)
.Options;
.......
.......
return _ => set.AsQueryable();
}
protected async Task<IRequestExecutor> CreateDb<T>(T[] Entities) where T : class
{
Func<IResolverContext, IQueryable<T>> resolver = await BuildResolverAsync(Entities);
return .......
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的工具(中队)提供了一种简便的方法,可以使
postgresqlresource
。此资源具有此属性:
idisposable
接口(或Xunit SpeficiaSynclifetime
接口)该对象可以以3方式以Xunit共享:
在您的情况下处置固定装置,您需要第三种方式。
因此,中队为您提供固定装置,Jou只需要定义
testCollection
来标记您的类。之后,您可以简单地使用属性标记测试类
[Collection(“ squadron”)]
,允许您通过构造函数进行注入共享实例。如果
postgresqlresource
还不够,您需要更复杂的固定装置非常容易;您可以在另一个方面创建自己的固定装置。当然,您需要实现相同的接口并将实现委托给内成员。
并参考
复杂fixture
在Xunit CollectionFixtures上设立的postgresqlresource
。不建议这种方法。在我看来,最好是注入测试课程的普通固定装置,而不是在需要的情况下包裹在类固定对象中。
因此,将此解决方案应用于您的代码可以如下。
Your tool (Squadron) provides an easy way to have a
PostgreSqlResource
.This resource has this properties:
IDisposable
interface (or xunit speficIAsyncLifetime
interface)This object can be shared in xunit in 3 way:
In your case you need the 3rd way.
So Squadron provide a fixture for you, jou just need to define a
TestCollection
to mark your classes.and after that you can simply tag your test classes with attribute
[Collection("Squadron")]
that allow you in inject via constructor the shared instance.In case
PostgreSqlResource
is not enought and you need a more complex fixture is very easy; you can just create your own fixture around the other.Of course you need to implement the same interface and delegate implementation to inner member.
And refer to
ComplexFixture
insted ofPostgreSqlResource
on xunit CollectionFixtures. This approach is not suggested.In my opinion is better a Plain fixture injected to test class, and than wrapped in a class fixture object if needed.
So applying this solution to your code can be as follows.