finder2
finder2

Reputation: 1084

verify with moq method is called with argument once

The class under test (A) calls a method of Object b. I'd like to verify the called Method is executed once with each defined parameter combination.

For the following code I'd like to verify CalledMethod is exactly once. Currently I verify the argument is in the list of arguments. But how to verify the arguments fit together and are only used once?

public void CalledMethod(string source, string target);

List<string[]> paths = new()
{
    new string[] { "incomingPath1", "targetPath1"},
    new string[] { "incomingPath2", "targetPath2" },
    new string[] { "incomingPath3", "targetPath3" },
};
// rearrange arguments
List<string> incomingPaths = ...
List<string> targetPaths = ...

// current verification codecode
mock.Setup(foo => foo.CalledMethod(It.IsIn<String>(incomingPaths), It.IsIn<String>(targetPaths), true));

// do stuff
...

// verify method is called once with each argument pair
// How?

Upvotes: 0

Views: 776

Answers (1)

Jonas H&#248;gh
Jonas H&#248;gh

Reputation: 10884

Simply call Verify with each of the exact argument pairs:

foreach (var path in paths) {
  mock.Verify(x => x.CalledMethod(path[0], path[1]), Times.Once());
}

Upvotes: 3

Related Questions