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 10 months ago

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

  2. Joel Abrahamsson's avatar

    Joel Abrahamsson 10 months ago

    Hey Polprav!

    Of course you can, as long as you don't copy the whole post and just pieces of it.

Add a comment

Allowed tags: <b>, <em>, <quote cite="">, <code>, <c-sharp-code>, <css-code>, <sql-code>, <xml-code>, <javascript-code>. If you want to display code examples, please remember to write &lt; for < and &gt; for >.

Follow me on Twitter

  1. Blogged: Learning Scala part nine – Uniform Access http://bit.ly/bmUat8 1 days ago
  2. @mikaellundin Your blog is a never ending source of wisdom. And weird mathematical problems. :) 3 days ago
  3. Bookmarked: Validate XHtml 1.0 Strict as part of your build process « Mint http://bit.ly/bOhaZj 3 days ago
follow me

Latest comments

  1. Svante wrote "Yes, I noticed that it was a singleton, and I guess the real..." on Something to beware of when using EPiAbstractions and an IoC container
  2. Joel Abrahamsson wrote "Well, first of all you wont get any arguments from me regard..." on Something to beware of when using EPiAbstractions and an IoC container
  3. Svante wrote "Hmm... Since the issue really is with the public instance co..." on Something to beware of when using EPiAbstractions and an IoC container

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