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 4109610
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:49:15+00:00 2026-05-20T21:49:15+00:00

I am using Rhino Mocks 3.6. I have seen many types of coding. Sometimes

  • 0

I am using Rhino Mocks 3.6. I have seen many types of coding. Sometimes using static GenerateMock method, sometimes using new MockRepository(). I don’t understand pretty well what is happening or what is better. Maybe some methods are obsolete, but anyway, let’s go to the real issue.

I would like to understand better what is happening in the code below and what is really needed to have a better test.

    [TestMethod]
    public void TestingProperty()
    {
        Person repository = MockRepository.GenerateMock<Person>();
        // tell rhino.mocks when FirstName is called in the next time, it should return "Monica"
        repository.Expect(x => x.Title).Return("Monica");
        // get the mocking ready
        repository.Replay();

        repository.VerifyAllExpectations();

        // when getting repository.Title value, we should receive "Monica" defined previously in expectation
        Assert.AreEqual(repository.Title, "Monica");
    }

I noticed when I remove repository.Replay(), everything keeps on working. What is the purpose of Replay, is it needed?

VerifyAllExpectations is also needed? What is it doing internally?

Can I avoid typing manually “Monica” and have a real mock object for Person?

If this is a bad code please let me know your suggestions!

  • 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-20T21:49:16+00:00Added an answer on May 20, 2026 at 9:49 pm

    It sounds like you haven’t worked with more recent mock object frameworks.

    In older “mock objects”, you have to do assertions manually against state of the mock object. For example, you’d run the code under test, which adds items to a list on your mock object. At the end of your test, you’d verify that the list on that mock object is populated correctly. This is verifying the state of the mock.

    This older style is like a less sophisticated version of a Rhino stub.

    With newer mock object frameworks, you stop verifying state of mock objects, and start verifying behavior. You make assertions about how your code under test calls your mock objects, not how properties/members are set.

    You’ll still do your classical assertions on the code under test. But you won’t do classical assertions on your mocks. You’ll instead set up expectations, and verify them with Rhino assertions.

    Some tips to correct this code:

    • There are two parts of the Rhino Mocks API being used here. Drop the record/playback portion, since it is more confusing, and stick to the AAA (Arrange, Act, Assert) syntax. See the release notes from when AAA was added
    • Stop testing mock objects directly
    • Test code that uses the mock object, and add expectations for what that code will call on the mock
    • Call your code under test before the call to VerifyAllExepectations, passing mocks to it as necessary
    • Ensure that the methods and properties you want to mock are marked virtual or abstract, or that your mock is based off an interface
    • Split your test cases into tests that verify mocks get called, and tests that verify state/return values are correct on the code under test
    • Don’t assert state on mocks at all, since you directly set it, and that just tests your test case

    Here’s some corrected example code:

    public class ObjectThatUsesPerson
    {
        public ObjectThatUsesPerson(Person person)
        {
            this.person = person;
        }
    
        public string SomeMethod()
        {
            return person.Title;
        }
    
        private Person person;
    }
    
    [TestMethod]
    public void TestingPropertyGotCalled()
    {
        // Arrange
        var mockPerson = MockRepository.GenerateMock<Person>();
        mockPerson.Expect(x => x.Title).Return("Monica");
        var someObject = new ObjectThatUsesPerson(mockPerson);
    
        // Act
        someObject.SomeMethod(); // This internally calls Person.Title
    
        // Assert
        repository.VerifyAllExpectations();
        // or: mockPerson.AssertWasCalled(x => x.Title);
    }
    
    [TestMethod]
    public void TestingMethodResult()
    {
        // Arrange
        var stubPerson = MockRepository.GenerateStub<Person>();
        stubPerson.Stub(x => x.Title).Return("Monica");
        var someObject = new ObjectThatUsesPerson(stubPerson);
    
        // Act
        string result = someObject.SomeMethod();
    
        // Assert
        Assert.AreEqual("Monica", result, "Expected SomeMethod to return correct value");
    }
    

    To test that this is working correctly, try these things (change the code back after each one):

    • Run it once and make sure it passes
    • Remove the Expect line, run it, and make sure it still passes (this isn’t a strict mock)
    • Add an extra Expect line, returning a different value. Run it, and make sure it fails
    • Add an extra SomeMethod call. Run it, and make sure it passes (not a strict mock)
    • Remove the code inside SomeMethod. Run it, and make sure it fails
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

a have some artefacts when using Rhino Mocks var mocks = new MockRepository(); INotifyMessageSender
I have been using Moq and initially Rhino Mocks over the last year together
How can I raise an event from a mock/stub using Rhino Mocks? I've found
I'm using MVC 2 with MVC contrib and Rhino mocks. I am in need
I am using .NET 4, NUnit and Rhino mocks. I want to unit test
Most of the time when we use Rhino mocks it works well, but we
I was playing around with testing using machine specifications and there is something that
Can this be done? Here is the method I'm testing: public void SaveAvatarToFileSystem() {
I'm currently building a class using TDD. The class is responsible for waiting for
How do I test for state in a S#arp Architecture project? For example, I

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.