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

  • Home
  • SEARCH
  • 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 7003183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:01:58+00:00 2026-05-27T21:01:58+00:00

I have the following sample test code public Stage Test(Stage Stage) { var StartStage

  • 0

I have the following sample test code

    public Stage Test(Stage Stage)
    {
        var StartStage = StageRepository.Single(x => x.Order == 1);
        var EndStage = StageRepository.Single(x => x.Order == 5);
        var ErrorStage = StageRepository.Single(x => x.Name == "Error");

        if (Stage == StartStage)
        {
            return EndStage;
        }
        else
        {
            return ErrorStage;
        }
    }

And I am trying to test it using the following unit test

    [TestMethod]
    public void XXXTest()
    {
        //// Arrange
        var AutoMocker = new RhinoAutoMocker<StageService>(MockMode.AAA);

        MockRepository mockRepository = new MockRepository();

        var MockStageRepository = AutoMocker.Get<IRepository<Stage>>();

        Stage StartStage = mockRepository.Stub<Stage>();
        StartStage.Order = 1;

        Stage EndStage = mockRepository.Stub<Stage>();
        EndStage.Order = 5;

        Stage ErrorStage = mockRepository.Stub<Stage>();
        ErrorStage.Name = "Error";

        System.Linq.Expressions.Expression<Func<Entities.Stage, bool>> StartParam = x => x.Order == 1;
        MockStageRepository
            .Stub(x => x.Single(Arg<System.Linq.Expressions.Expression<Func<Entities.Stage, bool>>>.Is.Equal(StartParam)))
            .Return(StartStage);

        System.Linq.Expressions.Expression<Func<Entities.Stage, bool>> EndParam = x => x.Order == 1;
        MockStageRepository
            .Stub(x => x.Single(Arg<System.Linq.Expressions.Expression<Func<Entities.Stage, bool>>>.Is.Equal(EndParam)))
            .Return(EndStage);

        System.Linq.Expressions.Expression<Func<Entities.Stage, bool>> ErrorParam = x => x.Order == 1;
        MockStageRepository
            .Stub(x => x.Single(Arg<System.Linq.Expressions.Expression<Func<Entities.Stage, bool>>>.Is.Equal(ErrorParam)))
            .Return(ErrorStage);

        StageService StageService = AutoMocker.ClassUnderTest;


        //Act
        var ReturnStage = StageService.Test(StartStage);


        //Assert
        Assert.AreEqual(ReturnStage, EndStage);
    }

However this is not working as it is not returning anything when I call StageRepository.Single(). If I change the stub code to ignore the argument then it does return something but it will be the same object returned for each call to Single() which I don’t want.

Is it possible to configure RhinoMocks in such a way as to return different objects from a stub depending on the lambda that is passed into it?

  • 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-27T21:01:59+00:00Added an answer on May 27, 2026 at 9:01 pm

    I think the root of your problem is that equality on the Expression<Func<T,U>> type is performed by reference rather than value. That means your telling Rhino Mocks to look for instances of expressions created in the test rather than the ones created in the code your testing.

    Two possible approaches come to mind:

    • One would be to provide a way to pass the lambda expressions in to the Stage class from the test, so that when the argument checks happen they are working against the same instances.

      Maybe something like:

      internal void SetStartStage(Expression<Func<Entities.Stage,bool>> predicate)
      {
          ...
      }
      

      The inverse of this would also work, i.e. provide the Expression objects as fields/properties that can be accessed by you’re test, and then use those when setting up your mock:

      internal Expression<Func<Entities.State,bool>> StartStagePredicate
      {
          get{ return x => x.Order == 1; }
      }
      
    • Another option would be to use the Matches method on Args to see if the argument checks the Stage object state correctly. This would require creating some Stage objects in your test that would match the criteria:

      var startStageTester = new Stage { Order = 1 };
      MockStageRepository
              .Stub(x => x.Single(Arg<System.Linq.Expressions.Expression<Func<Entities.Stage, bool>>>.Matches(y => y.Compile()(startStageTester)))
              .Return(StartStage);
      

      The call to Compile() is a little jarring, but since you’re dealing with an Expression and not a straight-up lambda, you’ve got to compile it in order to evaluate it.

      Now, if a Stage is something that is hard to create, then you may need to create a Mock/Stub of one (or use you’re StartStage) and have it return 1 from the Order property (in the case of the StartStage anyway).

    There are probably some others I’m not thinking of at the moment as well.

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

Sidebar

Related Questions

I have the following sample piece of code, public class test implements Runnable{ public
Say I have the following code: import java.lang.InterruptedException; import javax.swing.SwingWorker; public class Test {
I have this following test code: public static final String[] list = { apple,ball,cat,dog,egg,fan,girl,hat,igloo,jerk
I have the following test sample: <Window x:Class=WpfScrollTest.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=Window1 Height=200 Width=200> <Border>
I have the following sample code in a VB.NET console application. It compiles and
I have the following sample code which doesn't seem to want to run. import
I have the following sample data: Id Name Quantity 1 Red 1 2 Red
I have the following code sample that im trying to wrap my head around
I have a Dictionary bound to DataGridView by using following sample code. DataGridView bound
code sample taken from MSDN public class Test { public static void Main() {

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.