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 .......
}
}
Your tool (Squadron) provides an easy way to have a
PostgreSqlResource.This resource has this properties:
IDisposableinterface (or xunit speficIAsyncLifetimeinterface)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
TestCollectionto 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
PostgreSqlResourceis 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
ComplexFixtureinsted ofPostgreSqlResourceon 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.