在 xUnit 2.x 中的测试类上使用多个 collectionfixture

问题描述 投票:0回答:1

我正在为我的

DataAccessRepository
(使用实体框架)类编写测试用例。这个类在构造函数中有两个参数。

  1. 连接对象
  2. 自动映射器对象

现在,我在 xunit 中使用 collectionFixture 在我的测试类中传递 DatabaseFixture,但我还需要将 AutoMapper Fixture 传递给同一个测试类。我尝试依次添加两个集合,但它无效。有人可以阐明如何在 xunit 的测试类上使用多个 FixtureCollection 吗?

我的单元测试类如下所示,它抛出错误,因为我无法在类上使用两个 CollectionFixture 属性,

[Collection(Traits.DatabaseFixtureCollection)]
[Collection(Traits.AutomapperFixtureCollection)]
public class MyAssessmentRepositoryTests
{
    private readonly IMyAssessmentsRepository _Repo;
    public MyAssessmentRepositoryTests(DatabaseFixture dbFixture,AutomapperFixture amFixture)             
    {
        this._Repo = new MyAssessmentRepository(dbFixture.IcmDbContext,amFixture.Mapper);
    }
}
xunit xunit.net
1个回答
11
投票

参见https://xunit.net/docs/shared-context

单个测试类只能位于一个测试集合中(这就是为什么对属性有这样的约束)。

解决方案是声明一个“虚拟”测试集合,它声明这样一个集合中的测试应通过

ICollectionFixture<X>
s 控制访问的两个装置。

完成此操作后,测试类构造函数将根据需要配备任何 Fixture 实例。

(您还可以在测试类级别使用

IClassFixture
来声明集合之外的内容 [但此类夹具将向上/向下旋转] 每次在该测试类中执行测试,而不是在测试集合级别执行测试将在整个运行过程中向上/向下旋转一次,并在轮流访问集合夹具时将其交给集合中的所有测试类])

© www.soinside.com 2019 - 2024. All rights reserved.