Moq 函数调用总是返回值,即使输入与模拟输入值不匹配

Moq function call always returning value even though input doesn't match mocked input values

我正在模拟一个带有两个参数的函数调用。 1.输入不可变class对象 2.输出参数.

示例代码:

Mock<ISample> mockSample = new Mock<ISample>();

SampleClass MyKey = new SampleClass() { prop1 = 1 };
SampleOutput output = new SampleOutput() { prop2 = 2 };
mockSample.setup(s => s.SampleMethod(It.is<SampleKey>(t => t.Equals(MyKey)), 
out sampleOut))).Returns(true);

在实际代码执行中,如果键与模拟键相同,则此模拟函数 return 正确值,但是,我看到一个问题,即此模拟函数 return 输出值相同如果密钥不匹配。

有任何输入吗?

添加关键代码:

public class Key
{
    public readonly DateTime prop1;
    public readonly string prop2;
    public Key(DateTime prop1, string prop2)
    {
        this.prop1 = prop1;
        this.prop2 = prop2;
    }
    public override bool Equals(object obj)
    {
        if (obj == null)
            return false;

        if (ReferenceEquals(this, obj))
            return true;

        Key other = obj as Key;

        return this.prop1 == other.prop1 && string.Compare(this.prop2, other.prop2);
    }

    public override int GetHashCode()
    {
        return prop1.GetHashCode() ^ prop2.GetHashCode();
    }
}

据我了解这个问题,您需要针对两种不同的行为设置模拟。请查看示例测试:

[TestFixture]
public class MoqTests
{
    [Test]
    public void MoqOutParameter()
    {
        // Arrange
        Mock<ISample> mockSample = new Mock<ISample>();

        Key MyKey = new Key(DateTime.Today, "SomeValue");
        SampleOutput sampleOut = new SampleOutput() { prop2 = 2 };

        mockSample.Setup(s => s.SampleMethod(It.Is<Key>(t => t.Equals(MyKey)),
            out sampleOut)).Returns(true);

        // Act  
        SampleOutput out1;
        var result1 = mockSample.Object.SampleMethod(new Key(DateTime.Today, "SomeValue"), out out1);

        SampleOutput out2;
        var result2 = mockSample.Object.SampleMethod(new Key(DateTime.MinValue, "AnotherValue"), out out2);

        // Assert
        Assert.True(result1);
        Assert.AreEqual(out1, sampleOut);

        Assert.False(result2);
        Assert.Null(out2);
    }
}

public class Key
{
    public readonly DateTime prop1;
    public readonly string prop2;

    public Key(DateTime prop1, string prop2)
    {
        this.prop1 = prop1;
        this.prop2 = prop2;
    }
    public override bool Equals(object obj)
    {
        if (obj == null)
            return false;

        if (ReferenceEquals(this, obj))
            return true;

        Key other = obj as Key;

        // was forced to add `== 0` to make it compilable
        return this.prop1 == other.prop1 && string.Compare(this.prop2, other.prop2) == 0;
    }

    public override int GetHashCode()
    {
        return prop1.GetHashCode() ^ prop2.GetHashCode();
    }
}

public class SampleOutput
{
    public int prop2 { get; set; }
}

public interface ISample
{
    bool SampleMethod(Key key, out SampleOutput sampleOut);
}

UPD:我添加了 classes KeySampleOutput 和接口 'ISample',我在这个例子中使用了它,还围绕着 class 测试.我正在使用 Nunit 来启动测试,它一定没有意义。您可以根据需要使用任何单元测试框架。这个例子对我有用,测试是绿色的。请尝试一下,并说出与您的环境有何不同。另请注意,我已更改 Key.Equals 中的 return 行以使其可编译。

希望对您有所帮助。