Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Verify a method is called when I don't know what the parameter for the method will be in Moq

Tags:

c#

moq

I need to verify that a method is called, however it receives a parameter object which I can't determine at design time. I don't care what the parameter is, I just want to verify that the method is called.

So I would like to call something like this:

        var subDao = new Mock<ISubscriptionSnapshotDao>();
        subDao.Verify(x => x.Save(), Times.Exactly(1));

However ISubscriptionSnapshotDao.Save takes an object to save.

 Save(Subscription entity);

Is there a way verify that Save has been called without knowing what the parameter will be?

like image 459
Ross Scott Avatar asked Dec 22 '22 17:12

Ross Scott


1 Answers

Yes there is! If you know the Type of parameter the method expects.

It.IsAny<T>()

Try the following

subDao.Verify(x => x.Save(It.IsAny<Subscription>()), Times.Exactly(1));
like image 133
Peter Kelly Avatar answered Dec 29 '22 01:12

Peter Kelly