gpt4 book ai didi

c# - 运行所有测试时测试失败,但一个一个运行成功

转载 作者:行者123 更新时间:2023-11-30 16:56:43 24 4
gpt4 key购买 nike

我有 4 个测试分布在 3 个测试类中。如果我一个一个地运行每个测试,它们都会成功。但是运行所有(我认为是并行的?)除了第一个被解雇之外它们都失败了?

我的测试需要相同的设置,所以我有一个夹具,所有测试都用它来设置:

public class CompositionRootFixture
{
public Container Container { get; private set; } // Simple Injector Container

public CompositionRootFixture()
{
Container = new Container();

/* code removed for clearity */

Container.Verify();
}
}

并且像这样在我的测试类中使用:

public class CommandProcessorTests : IClassFixture<CompositionRootFixture>
{
private readonly CompositionRootFixture _fixture;

public CommandProcessorTests(CompositionRootFixture fixture)
{
_fixture = fixture;
}

[Fact]
public async Task TestExecutingUsingContainerForResolution()
{
var commands = _fixture.Container.GetInstance<IExecuteCommands>();
var command = new FakeCommandWithoutValidator();
await commands.Execute(command);

Assert.Equal("faked", command.ReturnValue);
}
}

我很难搞清楚如何使用 IClassFixture<T>并且文档对于设置它不是很有帮助。我使用的是最新的 XUnit 2.0.0-beta5-build2785。

失败说明:

---- System.InvalidOperationException : The configuration is invalid. Creating the instance for type IHandleCommand<FakeCommandWithoutValidator> failed. The registered delegate for type IHandleCommand<FakeCommandWithoutValidator> threw an exception. The configuration is invalid. The type HandleFakeCommandWithoutValidator is directly or indirectly depending on itself.
-------- SimpleInjector.ActivationException : The registered delegate for type IHandleCommand<FakeCommandWithoutValidator> threw an exception. The configuration is invalid. The type HandleFakeCommandWithoutValidator is directly or indirectly depending on itself.
------------ SimpleInjector.ActivationException : The configuration is invalid. The type HandleFakeCommandWithoutValidator is directly or indirectly depending on itself.
---- The following constructor parameters did not have matching fixture data: CompositionRootFixture fixture

最佳答案

您的容器正在利用一个单例,该单例在所有测试执行期间保持状态。

在每次测试之前初始化这个单例。

关于c# - 运行所有测试时测试失败,但一个一个运行成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27590856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com