OK, I am baffled at this point. Moq is not calling my one method, yet the assert on verify does show it is calling all my other methods that are not parameterless. I have even added the following:
.Throws<Exception>()
just to see if it would even throw, and still nothing (but adding it to other methods works). I know the object being used is my mock because I added a call that IS being logged immediately after the call that is not.
The method name is Finalize(). I doubt the naming of the method is the problem, but I have tried everything else.
Code After simplifying down to simplest solution by making the main method public:
var asyncRecognizerMock = new Mock<AsyncRecognizer>();
var asyncRecognizerFactoryMock = new Mock<AsyncRecognizerFactory>();
var trainerMock = new Mock<Trainer>();
trainerMock.Setup(trainer => trainer.Finalize()).Verifiable();
var trainerDataRepository = new TrainerDataRepository(asyncRecognizerFactoryMock.Object, asyncRecognizerMock.Object);
trainerDataRepository.FinalizeTrainer(trainerMock.Object);
trainerMock.Verify(trainer => trainer.Finalize(), Times.Once());
My method is now:
public void FinalizeTrainer(Trainer wordTrainer)
{
wordTrainer.Finalize();
}
Also, Moq is 4.0.10827.0 running against .Net 3.5
Rename your method to something other than
Finalize. If I copy/paste your code and rename the method, it starts to work fine.It also generates a compiler warning CS0465, and should be avoided.
The reason this doesn’t work is that
Finalizeis actually sort of a reserved name for the class destructor. If you write this C# code:The compiler actually names the destructor
Finalize()in the IL code:In fact if you try to add both a destructor and a Finalize method:
This code will no longer compile, because those 2 methods are the same thing. Tricky, eh? 🙂
It is also noted in ECMA-335: