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 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'm trying to use Rhinomocks 3.5 and the new lambda notation to mock some
I'm new to mock objects, but I understand that I need to have my
I am new to jmock and trying to mock an HttpSession. I am getting:
New class is a subclass of the original object It needs to be php4
New to WCF, but familiar with COM+ - can I wrap a WCF service
New to Linux programming in general. I am trying to communicate with a kernel
I am trying to mock out my Repository with Moq. I am trying to
I'm new to Python, but I used to work with ruby. I'm trying to
I'm using Sinatra (1.2) and RSpec (2.5) and would like to create a new
New to javascript/jquery and having a hard time with using this or $(this) to

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.