4

对于下面的代码,我得到这个断言失败并且不知道为什么:

Assert.AreEqual failed. Expected:<2>. Actual:<0>.

public interface IA
{
    void MethodA(B b);
}

public class A : IA
{
    public void MethodA(B b) {/*no matter*/}
}

public class B
{
    public string PropertyB { get; set; }
}

public class MyLogic
{
    private IA _a;
    public MyLogic(IA a)
    {
        _a = a;
    }
    public void DoLogic()
    {
        _a.MethodA(new B { PropertyB = "first" });
        _a.MethodA(new B { PropertyB = "second" });
    }
}

[TestClass]
public class MyLogicTests
{
    [TestMethod]
    public void CallTwiceAndCheckTheParams()
    {
        List<B> args = new List<B>();
        IA a = Substitute.For<IA>();

        new MyLogic(a).DoLogic();

        a.Received(2).MethodA(Arg.Do<B>(x => args.Add(x)));
        Assert.AreEqual(2, args.Count);
    }
}
4

1 回答 1

10

该代码正在设置一个要在调用完成后执行的操作 (Arg.Do)。我认为这就是你所追求的:

List<B> args = new List<B>();
IA a = Substitute.For<IA>();
a.MethodA(Arg.Do<B>(x => args.Add(x))); // do this whenever MethodA is called

new MyLogic(a).DoLogic();

a.Received(2).MethodA(Arg.Any<B>());
Assert.AreEqual(2, args.Count);
于 2013-10-09T23:01:47.667 回答