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

Follow me on Twitter

  1. @chraas You might want to use it for complex parts of the site and not use it for simple rendering pages. 2 days ago
  2. @chraas In theory more SOLID, but I'm not sure it's worth the price. 2 days ago
  3. @chraas Be warned that you're introducing one big chunk of complexity with EPiMVP :) 2 days ago
follow me

Latest comments

  1. Berra S wrote "Read your post at http://joelabrahamsson.com/entry/using-xfo..." on PageData objects not returned as typed when using Page Type Builder and FindPagesWithCriteria
  2. Linus wrote "1 up for behaviour being as close as expected as possible!" on A common problem with Page Type Builder and UniqueValuePerLanguage set to false
  3. Joel Abrahamsson wrote "Hi Hans, Could it be that you previously didn't have Page..." on Page Type Builder 2.0 released

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