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!
PS. For updates about new posts, sites I find useful and the occasional rant you can follow me on Twitter. You are also most welcome to subscribe to the RSS-feed.
This blog is built with EPiServer Community, EPiServer CMS, ASP.NET MVC and a bunch of other great products. The source code is available for download at the projects page, where you also can read more about this site and my other projects.
read more
Comments
Polprav 2 years ago
Hello from Russia!
Can I quote a post in your blog with the link to you?
Joel Abrahamsson 2 years ago
Hey Polprav!
Of course you can, as long as you don't copy the whole post and just pieces of it.
Daniel 1 years ago
Excellent post, you've just helped me with the same problem. Thanks very much!! :-D
Kevin 6 months ago
Thanks! I've done a google search for this issue a few times after forgetting and each time run into your blog post and remember