For some tests I want to share a fixture directly, other times I want to use it to build up a more complex fixture and pass in some parameters.
Ideally for me xUnit should use the default parameterless constructor if it's available instead of throwing an exception
```c#
public class ConfigurationFixture
{
public string ConnectionString { get; }
public ConfigurationFixture()
{
var timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
ConnectionString = $"Server=(localdb)\\mssqllocaldb;Database=Test_{timestamp};Trusted_Connection=True;MultipleActiveResultSets=true";
}
}
public class DatabaseFixture
{
public DatabaseFixture()
: this(new ConfigurationFixture())
{
}
public DatabaseFixture(ConfigurationFixture configFixture)
{
// Setup db with connection string
}
}
public class WebserverFixture
{
public WebserverFixture()
{
var configFixture = new ConfigurationFixture();
var dbFixture = new DatabaseFixture(configFixture);
// Setup webserver and database with same connection string well
}
}
public class UnitTests : IClassFixture<DatabaseFixture>
{
public UnitTests(DatabaseFixture dbFixture)
{
}
[Fact]
public void Run()
{
Assert.True(true);
}
}
public class IntegrationTests : IClassFixture<WebserverFixture>
{
public IntegrationTests(WebserverFixture serverFixture)
{
}
[Fact]
public void Run()
{
Assert.True(true);
}
}
throws an exception:
Test Name: Tests.UnitTests.Run
Test FullName: Tests.UnitTests.Run
Test Source: C:Users\Joao Sa\Source\Repos\TestsClass1.cs : line 51
Test Outcome: Failed
Test Duration: 0:00:00.001
Result StackTrace:
----- Inner Stack Trace #1 (Xunit.Sdk.TestClassException) -----
----- Inner Stack Trace #2 (Xunit.Sdk.TestClassException) -----
Result Message:
System.AggregateException : One or more errors occurred. (Class fixture type Tests.DatabaseFixture' may only define a single public constructor.) (The following constructor parameters did not have matching fixture data: DatabaseFixture dbFixture)
---- Class fixture type 'Tests.DatabaseFixture' may only define a single public constructor.
---- The following constructor parameters did not have matching fixture data: DatabaseFixture dbFixture
```
xUnit.net does not necessarily prefer the parameterless constructor, since there are ways to provide constructor arguments to fixtures (for example, for other fixtures).
@bradwilson: not related to this issue, but related to your comment, where can I find information on 'ways to provide constructor arguments to fixtures'?
@uberintj You can pass collection fixtures into the constructor of a class fixture. The code which does the mapping is here:
If I am using a class fixture like below, how to I pass this ClassFixtureMappings parameter? I am guessing override of some sort. An example would really help.
public sealed class MyTests: IClassFixture<TestFixture>, IDisposable
Most helpful comment
@bradwilson: not related to this issue, but related to your comment, where can I find information on 'ways to provide constructor arguments to fixtures'?