即使输入与模拟输入值不匹配,最小起订量函数调用也始终返回值

我正在模拟一个带有两个参数的函数调用。1. 输入不可变类对象 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);

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


有什么输入吗?


添加关键代码:


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();

    }

}


慕姐4208626
浏览 147回答 1
1回答

料青山看我应如是

据我了解这个问题,您需要在两种不同的行为上设置模拟。请看一下示例测试:[TestFixture]public class MoqTests{&nbsp; &nbsp; [Test]&nbsp; &nbsp; public void MoqOutParameter()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Arrange&nbsp; &nbsp; &nbsp; &nbsp; Mock<ISample> mockSample = new Mock<ISample>();&nbsp; &nbsp; &nbsp; &nbsp; Key MyKey = new Key(DateTime.Today, "SomeValue");&nbsp; &nbsp; &nbsp; &nbsp; SampleOutput sampleOut = new SampleOutput() { prop2 = 2 };&nbsp; &nbsp; &nbsp; &nbsp; mockSample.Setup(s => s.SampleMethod(It.Is<Key>(t => t.Equals(MyKey)),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out sampleOut)).Returns(true);&nbsp; &nbsp; &nbsp; &nbsp; // Act&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; SampleOutput out1;&nbsp; &nbsp; &nbsp; &nbsp; var result1 = mockSample.Object.SampleMethod(new Key(DateTime.Today, "SomeValue"), out out1);&nbsp; &nbsp; &nbsp; &nbsp; SampleOutput out2;&nbsp; &nbsp; &nbsp; &nbsp; var result2 = mockSample.Object.SampleMethod(new Key(DateTime.MinValue, "AnotherValue"), out out2);&nbsp; &nbsp; &nbsp; &nbsp; // Assert&nbsp; &nbsp; &nbsp; &nbsp; Assert.True(result1);&nbsp; &nbsp; &nbsp; &nbsp; Assert.AreEqual(out1, sampleOut);&nbsp; &nbsp; &nbsp; &nbsp; Assert.False(result2);&nbsp; &nbsp; &nbsp; &nbsp; Assert.Null(out2);&nbsp; &nbsp; }}public class Key{&nbsp; &nbsp; public readonly DateTime prop1;&nbsp; &nbsp; public readonly string prop2;&nbsp; &nbsp; public Key(DateTime prop1, string prop2)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.prop1 = prop1;&nbsp; &nbsp; &nbsp; &nbsp; this.prop2 = prop2;&nbsp; &nbsp; }&nbsp; &nbsp; public override bool Equals(object obj)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (obj == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; if (ReferenceEquals(this, obj))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; Key other = obj as Key;&nbsp; &nbsp; &nbsp; &nbsp; // was forced to add `== 0` to make it compilable&nbsp; &nbsp; &nbsp; &nbsp; return this.prop1 == other.prop1 && string.Compare(this.prop2, other.prop2) == 0;&nbsp; &nbsp; }&nbsp; &nbsp; public override int GetHashCode()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return prop1.GetHashCode() ^ prop2.GetHashCode();&nbsp; &nbsp; }}public class SampleOutput{&nbsp; &nbsp; public int prop2 { get; set; }}public interface ISample{&nbsp; &nbsp; bool SampleMethod(Key key, out SampleOutput sampleOut);}UPD:我添加的类Key,SampleOutput和接口“ISample”,我在这个实施例中使用,并且还sorrounding类的测试。我正在使用 Nunit 启动测试,它一定没有意义。您可以根据需要使用任何单元测试框架。这个例子对我有用,测试是绿色的。请尝试一下并说出与您的环境不同的地方。另请注意,我已更改返回行Key.Equals以使其可编译。希望能帮助到你。
打开App,查看更多内容
随时随地看视频慕课网APP