I have an object under test with a method called void Print(string msg)
.
The object invoke the method multiple times and passes a different message.
For example...
Strait forward of the usage:
public interface IMyPrinter
{
void Print(string msg);
}
public class Printer : IMyPrinter
{
public void Print(string msg)
{
// print to console
}
}
public class MyObject
{
public IMyPrinter Printer { get; set; }
public void foo()
{
for (var count = 0; count < 4; count++)
{
Printer.Print($"abc_{count}");
}
}
}
When I want to test foo
method, how can setup the Mock
object to capture the different Print
methods calls?
I tried:
var printerMock = new Mock<IMyPrinter>(MockBehavior.Loose);
for (var count = 0; count < 4; count++)
{
printerMock.Setup(_ => _.Print($"abc_{count}");
}
var underTest = new MyObject();
underTest.Printer = printerMock.Object;
underTest.foo();
printerMock.VerifyAll();
but this of course made only the last setup effective (when count
= 3).
How can this be done?