I just got a System.InvalidOperationException when setting up expectations for a mocked object using Rhino Mocks. The error message read: “Use Arg<T> ONLY within a mock method call while recording. 1 arguments expected, 2 have been defined.”.
This was like the gazillionth time this has happened to me so I’m writing down the quite obvious solution here.
My code always looks something like this:
MockRepository fakes = new MockRepository();
RecipeUpdater recipeUpdater = fakes.PartialMock<RecipeUpdater>();
recipeUpdater.Stub(updater =>
updater.AddNonExistingCategories(
Arg<ICategory>.Is.Anything,
Arg<List<string>>.Is.Anything));
fakes.ReplayAll();
I always find myself doing the same thing when this happens. First I stare at the code for the test until my eyes bleed. Then I ask Google for a solution. Google always directs me to this blog entry. I try that solution only to get a null reference exception from the code that I’m trying to test. Then, depending on how tired I am, it takes me 0 to 30 minutes to realize that the reason that both exceptions are thrown is that I’ve forgotten to make the method I’m testing virtual. Doh!