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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:07:32+00:00 2026-05-16T16:07:32+00:00

I am new to mock objects, and I am trying to learn how to

  • 0

I am new to mock objects, and I am trying to learn how to use them in RSpec. Can someone please post an example (a hello RSpec Mock object world type example), or a link (or any other reference) on how to use the RSpec mock object API?

  • 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-16T16:07:32+00:00Added an answer on May 16, 2026 at 4:07 pm

    Here’s an example of a simple mock I did for a controller test in a rails application:

    before(:each) do
      @page = mock_model(Page)
      @page.stub!(:path)
      @page.stub!(:find_by_id)
      @page_type = mock_model(PageType)
      @page_type.stub!(:name)
      @page.stub!(:page_type).and_return(@page_type)
    end
    

    In this case, I’m mocking the Page & PageType models (Objects) as well as stubbing out a few of the methods I call.

    This gives me the ability to run a tests like this:

    it "should be successful" do
      Page.should_receive(:find_by_id).and_return(@page)
      get 'show', :id => 1
      response.should be_success
    end
    

    I know this answer is more rails specific, but I hope it helps you out a little.


    Edit

    Ok, so here is a hello world example…

    Given the following script (hello.rb):

    class Hello
      def say
        "hello world"
      end
    end
    

    We can create the following spec (hello_spec.rb):

    require 'rubygems'
    require 'spec'
    
    require File.dirname(__FILE__) + '/hello.rb'
    
    describe Hello do
      context "saying hello" do 
        before(:each) do
          @hello = mock(Hello)
          @hello.stub!(:say).and_return("hello world")
        end
    
        it "#say should return hello world" do
          @hello.should_receive(:say).and_return("hello world")
          answer = @hello.say
          answer.should match("hello world")
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to Mock the MVC object HttpPostedFileWrapper so I can test the
Im trying to mock a new File generated in a loop. Simplified example: class
I am trying folow the example from Mock Objects in Play[2.0] but unfortunately I
Is it possible to do some form of expect NEW in rhino mock. Example:
I'm trying to create a mock HttpContextBase for unit test. var fakePrinciple = new
new to c#. I'm trying to make a simple system where I can search
I´m encountering this problem trying to mock some objects that receive complex lambda expressions
I've been exploring the use of mock objects in unit testing and have been
Trying to use NUnit to test a method that adds an object to a
I am trying to Mock a HttpResponse for a ConrollerContext object using MVC 4,

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.