Rhino Mocks - Use Arg ONLY within a mock method call while recording

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.

Comments

  1. Polprav's avatar

    Polprav 2 years ago

    Hello from Russia!
    Can I quote a post in your blog with the link to you?

  2. Joel Abrahamsson's avatar

    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.

  3. Daniel's avatar

    Daniel 1 years ago

    Excellent post, you've just helped me with the same problem. Thanks very much!! :-D

  4. Kevin's avatar

    Kevin 9 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

Follow me on Twitter

  1. @unclebobmartin Because code coverage is a number that management can measure? 13 hours ago
  2. I'm amazed. "oikeinkirjoitusehdotuksista" is an actual word in the Finnish language! 13 hours ago
  3. @tednyberg Amen to that! 14 hours ago
follow me

Latest comments

  1. Per Ivansson wrote "We will definitely try to release as continiously as we poss..." on On selling 200OK and Truffler to EPiServer
  2. Joel Abrahamsson wrote "Thanks Andreas! Regarding your questions it's not really ..." on On selling 200OK and Truffler to EPiServer
  3. Andreas R wrote "Congrats on the sale. Hope ur rolling around in cash now ;) ..." on On selling 200OK and Truffler to EPiServer

About this site

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