Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6950037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:02:31+00:00 2026-05-27T14:02:31+00:00

I am stubbing two different objects. Here are the interfaces: public IInterface1 { IEnumerable<Foo>

  • 0

I am stubbing two different objects. Here are the interfaces:

public IInterface1
{
    IEnumerable<Foo> Method1(int p1, string p2 = null, string p3 = null);
}

public IInterface2
{
    Bar Method2(int p3, int? p4 = null);
}

As you can see, both have parameters that default to null. However, one is matching my Expect, and the other isn’t. This stub works; when its method is called with a value matching testData.p1, it returns an array of two Foos:

var obj1 = MockRepository.GenerateStub<IInterface1>();
obj1.Expect(m => m.Method1(
         Arg.Is(testData.p1), 
         Arg<string>.Is.Null, 
         Arg<string>.Is.Null))
    .Return(new[] { new Foo(), new Foo() });

However, this similar stub is not returning the expected result, returning null instead, despite the parameters matching the expectation:

var obj2 = MockRepository.GenerateStub<IInterface2>();
obj2.Expect(m => m.Method2(Arg.Is(testData.p3), Arg<int?>.Is.Null))
    .Return(new Bar());

Why is this second stub not matching on my parameters? Even if I use Arg<int?>.Is.Anything for the second expected parameter, null is still returned.

Update

OK, I found an alternate way to set up my expectation, passing the expected values instead of using Arg:

obj1.Expect(m => m.Method1(testData.p1, null, null))
    .Return(new[] { new Foo(), new Foo() });

obj2.Expect(m => m.Method2(testData.p3, null))
    .Return(new Bar());

Both stubs return the expected results when I set them up this way. I’m still unclear on the distinction and why one worked with Arg and the other didn’t. I suppose this is yet another time where I’m getting confused about the mixing of AAA, record/replay, mocks, and stubs in this library. Can anyone dispel my confusion about why the original code didn’t work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:02:32+00:00Added an answer on May 27, 2026 at 2:02 pm

    At runtime, when you pass a null to the second parameter of IInterface2, .NET will create a Nullable<int> and pass that to your method implementation. Your implementation code can then check p4.HasValue and do whatever you want to account for null values.

    But from Rhino.Mocks standpoint, when it intercepts the method call, it’s going to see that the second parameter is a Nullable<int> (not a null instance) and therefore you won’t match on your expectation.

    In the second attempt, when you pass null directly, the runtime will, again, wrap that null up in a Nullable<T>. Rhino.Mocks will check the two parameters for equality (to see if you have a matching expectation). Nullable<T> overrides the Equals method to check if both of the Nullable<T>‘s have null and will return true — thus matching your expectation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a recent question on stubbing, many answers suggested C# interfaces or delegates for
I have two pointers to objects and I want to test if they are
I am looking for python stubbing library. Something that could be used to create
Within my controller specs I am stubbing out valid? for some routing tests, (based
I am currently struggling with stubbing out a helper method of my Sinatra app
Im new to tdd and stubbing. When I stub a method im assumng that
So I'm trying to figure out a way of stubbing a controller method in
I'm using rspec and the rspec mocks library for stubbing/mocking, but I'm starting to
The following test case fails in rhino mocks: [TestFixture] public class EnumeratorTest { [Test]
I've read a lot about mocking/stubbing/faking - and still hit my mental roadblocks. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.