c# - Passing optional parameter to class fixture constructor in xunit - Stack Overflow

I have tests that use an existing fixture, and I want to add new tests that pass a parameter to the fix

I have tests that use an existing fixture, and I want to add new tests that pass a parameter to the fixture for configuration. I want to use optional parameters to avoid breaking the existing tests. Unfortunately...

Tests that don't specify the fixture with arguments get this error: Class fixture type 'ContextFixture' had one or more unresolved constructor arguments: String options

public class ContextFixture : IDisposable
{
    public ContextFixture(string? options = default) {...}
}
public class SandboxContextFixture : ContextFixture, IDisposable
{
    public SandboxContextFixture() : base(options) {...}
}
public class ServiceTests : IClassFixture<ContextFixture>
{
    public ServiceTests(ContextFixture contextFixture) {...}
}
public class RepositoryTests : IClassFixture<SandboxContextFixture>
{
    public RepositoryTests(SandboxContextFixture fixture) {...}
}

I have tests that use an existing fixture, and I want to add new tests that pass a parameter to the fixture for configuration. I want to use optional parameters to avoid breaking the existing tests. Unfortunately...

Tests that don't specify the fixture with arguments get this error: Class fixture type 'ContextFixture' had one or more unresolved constructor arguments: String options

public class ContextFixture : IDisposable
{
    public ContextFixture(string? options = default) {...}
}
public class SandboxContextFixture : ContextFixture, IDisposable
{
    public SandboxContextFixture() : base(options) {...}
}
public class ServiceTests : IClassFixture<ContextFixture>
{
    public ServiceTests(ContextFixture contextFixture) {...}
}
public class RepositoryTests : IClassFixture<SandboxContextFixture>
{
    public RepositoryTests(SandboxContextFixture fixture) {...}
}
Share Improve this question asked Jan 17 at 17:18 Coden00bCoden00b 3522 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

xUnit has some strict rules about which type of parameters can be accepted in the public constructor of a fixture class; a IMessageSink and ITestContextAccessor. See source code on GitHub. Note that exception being thrown here because the string option in your code is not supported.

To resolve, keep a default/parameterless public constructor on that ContextFixture class allowing it to be instantiated without parameters.

Add a protected constructor with parameters to that ConstextFixture and let the derived SandboxContextFixture fixture class call that one from its default/parameterless constructor.

public class ContextFixture
{
    public ContextFixture()
    { }

    protected ContextFixture(string? options)
    { }
}

public class SandboxContextFixture : ContextFixture
{
    public SandboxContextFixture() : base("some options value")
    { }
}

Optionally, in the ContextFixture class, the parameterless constructor can call the one with parameters.

public class ContextFixture
{
    public ContextFixture() : this(default)
    { }

    protected ContextFixture(string? options)
    {
        // ... 
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745353061a4623953.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信