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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:24:37+00:00 2026-05-18T09:24:37+00:00

I think I may be biting off more than I can chew by trying

  • 0

I think I may be biting off more than I can chew by trying to learn RSpec on my own…there doesn’t appear to be any complete documentation on RSpec 2?!? at least none that I have been able to find…

Anyway, I, in a feeble attempt to integrate RSpec, started by examining the 29 specs created by the scaffold generator in Rails 3. Some I understand but much still escapes me. Hoping someone can help (or point me to decent RSpec 2 documentation).

For example in the first code block below (def mock_clown...) I can assume mock_clown is creating a mock of my Clown class to test against. But what exactly is going on? what is generating this “mock”? what does .as_null_object and .tap mean? This is very confusing as I can’t find an example to compare this block to in any docs or tutorials I’ve come across…

#clowns_controller_spec.rb

require 'spec_helper'

describe ClownsController do

  def mock_clown(stubs={})
    (@mock_clown ||= mock_model(Clown).as_null_object).tap do |clown|
      clown.stub(stubs) unless stubs.empty?
    end
  end

  describe "GET index" do
    it "assigns all clowns as @clowns" do
      Clown.stub(:all) { [mock_clown] }
      get :index
      assigns(:clowns).should eq([mock_clown])
    end
  end

... 


  describe "POST create" do

...


    describe "with invalid params" do
      it "assigns a newly created but unsaved clown as @clown" do
        Clown.stub(:new).with({'these' => 'params'}) { mock_clown(:save => false) }
        post :create, :clown => {'these' => 'params'}
        assigns(:clown).should be(mock_clown)
      end

      it "re-renders the 'new' template" do
        Clown.stub(:new) { mock_clown(:save => false) }
        post :create, :clown => {}
        response.should render_template("new")
      end
    end

...

end
  • 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-18T09:24:37+00:00Added an answer on May 18, 2026 at 9:24 am

    The best source of documentation for rspec is probably its github wikis. Here’s a general link to all gems:https://github.com/rspec/rspec/wiki. Also, checkout the rdoc, linked to here.

    As for specific answers, as_null_object causes the mock to record and ignore all method calls. (This is great, as you don’t have to spec out every single method on an object, just the ones you care about.)

    Tap is a Ruby 1.9 feature. From the documentation at this link:

    Object#tap

    Passes the object to the block and returns it (meant to be used for call chaining).

    For learning Rspec 2 Webrat and Cucumber on your own, I definitely recommend checking out the RSpec Book. Awesome resource, and covers all aspects.

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

Sidebar

Related Questions

I think I may misunderstand why the with command is used. But can any
I think this may sound pretty simple, but still I can not get it
I've found what I think may be a bug with Ivar and Objective-C runtime.
You may think this question is like this question asked on StackOverflow earlier. But
I think I may have a misunderstanding of <xsl:variable\> and <xsl:value-of\> so perhaps someone
I think I may have accidently disabled stylecop in my Visual Studio 2008 enviroment.
I think I may be a bit confused on the syntax of the Moq
I think we may have trouble with our existing project. For some reasons we
I think I may have used a repeater when I should have used something
I think I may be way in over my head here... I'm using a

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.