For the code bellow I get this assert failure and do not know why:
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);
}
}
The code is setting up an action to perform (Arg.Do) after the calls have already been made. I think this is what you are after:
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With