Maxim Konyuh
Maxim Konyuh

Reputation: 35

Verify multiple invocations with different parameters of some method using moq

I have some repository and I have a method which I would like to test which is invoked in the loop with parameter as an array. This method is taking values from array by 100 items at a time. So if I have an array with 434 items method should be invoked 5 times.

So how exactly can I test this method if e.g. I need to verify this method to be invoked 5 times with passing as a parameter array with 434 items?

var items = GetListOfStrings(434); // return list with 434 items

context.Mock<ISomeRepository>()
            .Verify(method => method.GetSomeItems(It.IsAny<string[]>(), Times.Exactly(5)));

Right now I'm passing as a parameter It.IsAny<string>(), but I would like to pass items and take next 100 items per one method invocation. Is it possible?

Upvotes: 3

Views: 4178

Answers (1)

Dariusz Woźniak
Dariusz Woźniak

Reputation: 10350

You may pass ranges of items and verify calls by comparing if two arrays contains the same elements (using SequenceEqual):

List<string> items = GetListOfStrings(494);

Mock.Get(someRepository).Verify(repository => repository.GetSomeItems(It.Is<string[]>(strings => strings.SequenceEqual(items.GetRange(0, 100)))), Times.Once);
Mock.Get(someRepository).Verify(repository => repository.GetSomeItems(It.Is<string[]>(strings => strings.SequenceEqual(items.GetRange(100, 100)))), Times.Once);
Mock.Get(someRepository).Verify(repository => repository.GetSomeItems(It.Is<string[]>(strings => strings.SequenceEqual(items.GetRange(200, 100)))), Times.Once);
// ...

Upvotes: 5

Related Questions