【问题标题】:Can XUnit interfere with the code?XUnit 会干扰代码吗?
【发布时间】:2016-04-11 19:33:47
【问题描述】:

我最近有一个非常奇怪的行为,很奇怪,我开始怀疑罪魁祸首是否可能是我正在使用的单元框架(XUnit)。我在那里问了一个问题:Can Expressmapper copy to destination? 但这个问题不再是关于 Expressmapper 而是关于 XUnit。

你知道 XUnit 是否会以某种方式干扰代码吗?

这是我要问的原因:

我可以以任何顺序“一起或分开”运行它们,但我总是会出现这种疯狂的行为:

  • 第一次测试失败 (Test_xxx)
  • 第二次测试通过 (Test_Map)

两个测试包含完全相同的代码!!

    [Fact]
    [Trait("Test kind", "Integration")]
    public void Test_xxx()
    {
        // Arrange
        var mapper = new MappingServiceProvider();
        mapper.Register<MapSource, MapDestination>();
        var src = new MapSource
        {
            Id = Guid.NewGuid().GetHashCode(),
            Guid = Guid.NewGuid(),
            Parent = new MapSource
            {
                Id = Guid.NewGuid().GetHashCode(),
                Guid = Guid.NewGuid()
            }
        };
        src.Children = Enumerable.Range(0, 3).Select(i => new MapSource
        {
            Id = Guid.NewGuid().GetHashCode(),
            Guid = Guid.NewGuid()
        }).ToList();
        var dst = new MapDestination();

        // Act
        mapper.Map(src, dst);

        // Assert
        var compare = new CompareLogic(new ComparisonConfig
        {
            IgnoreObjectTypes = true
        });
        var comparison = compare.Compare(src, dst);
        Assert.Equal(new List<Difference>(), comparison.Differences);
    }

    [Fact]
    [Trait("Test kind", "Integration")]
    public void Test_Map()
    {
        // Arrange
        var mapper = new MappingServiceProvider();
        mapper.Register<MapSource, MapDestination>();
        var src = new MapSource
        {
            Id = Guid.NewGuid().GetHashCode(),
            Guid = Guid.NewGuid(),
            Parent = new MapSource
            {
                Id = Guid.NewGuid().GetHashCode(),
                Guid = Guid.NewGuid()
            }
        };
        src.Children = Enumerable.Range(0, 3).Select(i => new MapSource
        {
            Id = Guid.NewGuid().GetHashCode(),
            Guid = Guid.NewGuid()
        }).ToList();
        var dst = new MapDestination();

        // Act
        mapper.Map(src, dst);

        // Assert
        var compare = new CompareLogic(new ComparisonConfig
        {
            IgnoreObjectTypes = true
        });
        var comparison = compare.Compare(src, dst);
        Assert.Equal(new List<Difference>(), comparison.Differences);
    }

【问题讨论】:

    标签: xunit


    【解决方案1】:

    我有另一种称为 Test_Map 的方法(具有不同的签名;又名重载)。

    我猜 XUnit 要求方法名称是唯一的(我打算这样做)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-15
      • 2017-12-23
      • 1970-01-01
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      • 2012-08-08
      • 1970-01-01
      相关资源
      最近更新 更多