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!
- Setting Expectations With StructureMap’s MoqAutoMocker
- Using MSpec – a few weeks in
- Web testing with Selenium and xUnit.net
- I have a crush on MSpec
- Best practices when testing large but simple methods?
- A neat little type inference trick with C#
- Manage multiple web.config files using Phantom
- Getting property and method names using static reflection in C#