Using Moq.It.IsAny to test a string starts with something

.net, c#, moq, unit-testing

Solution

try:

logger.Verify(x => x.WriteData(Moq.It.Is<string>(str => str.StartsWith("ABC"))), Times.Exactly(3));

you can see another example of It.Is:

// matching Func<int>, lazy evaluated
mock.Setup(foo => foo.Add(It.Is<int>(i => i % 2 == 0))).Returns(true); 

that comes from Moq documentation: https://github.com/Moq/moq4/wiki/Quickstart

Problem

Is it possible to use Moq to say a method accepts a string that starts with "ABC" for example. As an example something like this: ``` logger.Verify(x => x.WriteData(Moq.It.IsAny<string>().StartsWith("ABC")), Times.Exactly(3)); ``` That wont compile but hopefully it illustrates my point

Original source