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
It. IsAny<T> is checking that the parameter is of type T, it can be any instance of type T. It's basically saying, I don't care what you pass in here as long as it is type of T.
Moq, how it works The idea is to create a concrete implementation of an interface and control how certain methods on that interface responds when called. This will allow us to essentially test all of the paths through code.
When placing an order with some manufacturers and wholesalers, they may require a minimum order quantity (MOQ). That means sometimes manufacturers, suppliers and wholesalers will turn away some customers if they are not willing or able to meet the minimum order quantity.
Moq is a mocking framework for C#/. NET. It is used in unit testing to isolate your class under test from its dependencies and ensure that the proper methods on the dependent objects are being called. For more information on mocking you may want to look at the Wikipedia article on Mock Objects.
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
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