Search code examples
c#moqautofixtureteststack

check that a function had been called with a certain parameter in unit testing


I'm working with BDDfy, AutoFixture and Moq for my testing.

I want to check that a function had been called with a certain parameter.

The function has a number of parameters which it gets.

Updated This is the given step

 ICollection<string> result = new List<string>();
    _objectMock = new Mock<T>();
    _objectMock.SetupSequence(objectMock =>
     objectMock.Run(It.IsAny<DataModel1>(), It.IsAny<Collection<DataModel2>>())
     .Throws(_customException)
     .Returns(Task.FromResult(result));

On the [Then] part, I check that:

    mock.Verify(mock => mock.verifyData(It.IsAny<DataModel1>(), It.IsAny<IReadOnlyCollection<DataModel2>>()));

I want to check that the 2nd parameter to the function verifyData was of a certain object.


Solution

  • You could use It.Is<T> for the purpose. For example,

    It.Is<string>(c=>c == "Something")
    

    In the particular case you have given in OP, the second parameter could be given as

    mock.Verify(mock => mock.verifyData(It.IsAny<DataModel1>(), It.Is<IReadOnlyCollection<DataModel2>>(x=> x==expectedParam), It.IsIn<IReadOnlySet<DataModel3>>(), It.IsAny<IReadOnlyCollection<DataModel3>>()))