gpt4 book ai didi

NSubstitute:Arg.Do 不满足调用参数列表

转载 作者:行者123 更新时间:2023-12-01 08:59:58 26 4
gpt4 key购买 nike

对于下面的代码,我得到了这个断言失败,不知道为什么:
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);
}
}

最佳答案

该代码正在设置一个要在调用完成后执行的操作 (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);

关于NSubstitute:Arg.Do 不满足调用参数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19275164/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com