0

我正在使用起订量和 AutoFixture。

给定以下接口:

public interface Int1
{
    Int2 Int2 { get; }
}

public interface Int2
{
    string Prop1 { get; }
    string Prop2 { get; }
}

我正在执行这样的测试:

using AutoFixture;
using AutoFixture.AutoMoq;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

[TestClass]
public class TestClass
{
    [TestMethod]
    public void Test1()
    {
        var f = new Fixture().Customize(new AutoMoqCustomization { ConfigureMembers = true });

        var obj = f.Create<Mock<Int1>>();

        obj.Object.Int2.Prop1.Should().NotBeNullOrEmpty();
        obj.Object.Int2.Prop2.Should().NotBeNullOrEmpty();
    }

    [TestMethod]
    public void Test2()
    {
        var f = new Fixture().Customize(new AutoMoqCustomization { ConfigureMembers = true });

        var obj = f.Create<Mock<Int1>>();

        obj.Setup(q => q.Int2.Prop1).Returns("test");

        obj.Object.Int2.Prop1.Should().Be("test");
        obj.Object.Int2.Prop2.Should().NotBeNullOrEmpty();
    }
}

第一个测试通过,第二个测试失败:Expected obj.Object.Int2.Prop2 not to be <null> or empty, but found <null>. 似乎在它的依赖属性之一上使用 Setup 时Int2会清除整个Int2对象(将所有属性设置为默认值)。这是为什么?如何避免?

obj.Object创建后看起来像这样:

设置前

但执行后Setup它看起来像这样(Prop2is null):

设置后

有趣的是,当我Int2在创建属性后访问它时,它工作正常。所以这个测试通过了(变量int2不在任何地方使用):

    [TestMethod]
    public void Test2()
    {
        var f = new Fixture().Customize(new AutoMoqCustomization { ConfigureMembers = true });

        var obj = f.Create<Mock<Int1>>();

        var int2 = obj.Object.Int2;

        obj.Setup(q => q.Int2.Prop1).Returns("test");

        obj.Object.Int2.Prop1.Should().Be("test");
        obj.Object.Int2.Prop2.Should().NotBeNullOrEmpty();
    }

有任何想法吗?

这也是一个 .csproj 文件供参考:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net5.0</TargetFramework>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="AutoFixture" Version="4.15.0" />
        <PackageReference Include="AutoFixture.AutoMoq" Version="4.15.0" />
        <PackageReference Include="FluentAssertions" Version="5.10.3" />
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
        <PackageReference Include="Moq" Version="4.16.1" />
        <PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
        <PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
    </ItemGroup>
</Project>
4

1 回答 1

1

简而言之,在为Int2属性设置返回值之后,您最终会得到该属性的不同模拟实例Prop1。为了满足您的请求, Moq将生成一个新的模拟,它将返回Prop1.

您可以将其视为等效于以下测试:

[Fact]
public void Test3()
{
    var obj2 = new Mock<IInterfaceB>();
    obj2.Setup(x => x.Property1).Returns(Guid.NewGuid().ToString());
    obj2.Setup(x => x.Property2).Returns(Guid.NewGuid().ToString());

    var obj = new Mock<IInterfaceA>();
    obj.Setup(x => x.PropertyB).Returns(obj2.Object);

    obj.Setup(q => q.PropertyB.Property1).Returns("test");

    Assert.Equal("test", obj.Object.PropertyB.Property1);
    Assert.NotEmpty(obj.Object.PropertyB.Property2);
}

如果您想保留初始模拟实例并进行更改,Prop1那么您可以使用Mock.Get<T>(T).

[Fact]
public void Test4()
{
    var f = new Fixture().Customize(new AutoMoqCustomization { ConfigureMembers = true });

    var obj = f.Create<Mock<IInterfaceA>>();

    var obj2 = Mock.Get(obj.Object.PropertyB);
    obj2.Setup(q => q.Property1).Returns("test");

    Assert.Equal("test", obj.Object.PropertyB.Property1);
    Assert.NotEmpty(obj.Object.PropertyB.Property2);
}

但我会推荐使用 AutoFixture 的冻结功能

[TestMethod]
public void Test5()
{
    var fixture = new Fixture()
        .Customize(new AutoMoqCustomization { ConfigureMembers = true });

    var int2Mock = fixture.Freeze<Mock<Int2>>();
    var int1Mock = fixture.Create<Mock<Int1>>();

    int2Mock.Setup(q => q.Prop1).Returns("test");

    int1Mock.Object.Int2.Prop1.Should().Be("test");
    int1Mock.Object.Int2.Prop2.Should().NotBeNullOrEmpty();
}
于 2021-03-02T23:19:28.950 回答