3

The situation is that I have a test class that needs a CollectionFixture injected, this latter needs another one to be injected to it also. This is not working.

This is my collection definition

[CollectionDefinition(nameof(AssemblyFixtures ))]
public class AssemblyFixtures :
    ICollectionFixture<DataMock>,
    ICollectionFixture<ItemCatalogSearchApiFactory>
{
}

And I have a test class :

[Collection(nameof(AssemblyFixtures))]
    public class TestBase
    {
        private readonly ItemCatalogSearchApiFactory _apiFactory;
        private readonly ITestOutputHelper _outputHelper;

        public TestBase(ItemCatalogSearchApiFactory apiFactory, ITestOutputHelper outputHelper)
        {
            _apiFactory = apiFactory;
            _outputHelper = outputHelper;
        }
    }

and ItemCatalogSearchApiFactory that needs DataMock

[Collection(nameof(AssemblyFixtures))]
public class ItemCatalogSearchApiFactory : WebApplicationFactory<Startup>
{
    public ItemCatalogSearchApiFactory(DataMock dataMock)
    {            
        //using DataMock for some reason here
    }
}

This doesn't work and it fails to instantiate ItemCatalogSearchApiFactory with the following error thrown :

Message:  System.AggregateException : One or more errors occurred. (Collection fixture type 'XXXXXXX.IntegrationTests.ItemCatalogSearchApiFactory' had one or more unresolved constructor arguments: DataMock dataMock) (The following constructor parameters did not have matching fixture data: ItemCatalogSearchApiFactory apiFactory) ---- Collection fixture type 'XXXXXXX.IntegrationTests.ItemCatalogSearchApiFactory' had one or more unresolved constructor arguments: DataMock dataMock ---- The following constructor parameters did not have matching fixture data: ItemCatalogSearchApiFactory apiFactory Stack Trace:  ----- Inner Stack Trace #1 (Xunit.Sdk.TestClassException) ----- ----- Inner Stack Trace #2 (Xunit.Sdk.TestClassException) -----

Is this the normal behavior ? Any Idea how should correct this situation ?

AymenDaoudi
  • 7,811
  • 9
  • 52
  • 84

1 Answers1

1

xunit doesn't support dependent fixtures, it won't resolve order of fixture instantiation.

Apart from that, I see here misuse of ICollectionFixure interface. xunit has 3 ways of managing shared state in tests:

  • IDisposable interface - xunit instantiates a new test class instance for each Fact attributed method in the test class, so code that will be put inside Constructor and Dispose method will be executed before each and after each test method correspondingly. It has IAsyncLifetime counterpart for async code if needed.

  • IClassFixure interface - used to inject fixture that are defined with IDisposable\IAsyncLifetime in a test class code. xunit will inject an instance of SomeClass in a test class marked with IClassFixture<SomeClass> which will be SHARED ACROSS ALL TESTS in that test class.

  • ICollectionFixture together with CollectionDefinition and Collection is used to define shared state across MULTIPLE TEST CLASSES.

xunit docs on shared state

If you really need to have dependent fixtures, you'll have to compose them together in a separate fixture and that new fixture use with ICollectionFixture or IClassFixture depending on your case.

Monsieur Merso
  • 1,459
  • 1
  • 15
  • 18