Search code examples
ruby-on-rails-3mockingfindrspec2stubbing

Rspec 2 and Rails 3 stubbing / mocking


I am currently in the process of migration to rails 3 from rails 2 in a large application. In our functional specs, we have alot of stuff like this:

@model = Factory :model
@child = Factory :child
Model.stub!(:find).and_return(@model)
Child.stub!(:find).and_return(@child)

...

@child.should_receive(:method).twice

The main issue is that if I let it hit DB and get actual instance of child, real :method makes tests too complex (need two big factories) and slow.

In code we use various ways to get items: find, dynamic finders, etc

@model = Model.find(1)    
@child = @model.children.find_by_name(name)

How would you advice to move this logic to rails 3? Any advice on another stubbing/mocking library maybe?


Solution

  • Normally you would mock the model inside controller specs:

    Model.stub!(:find).and_return(mock_model('Model'))
    Child.stub!(:find).and_return(mock_model('Child'))
    

    However, when you've got gem "rspec-rails", "~> 2.0" in your rails 3 app's Gemfile, then the standard rails scaffold generator will use rspec to generate specs for you, so running rails generate scaffold MyResource will generate some example specs for you.

    The following is a lightly annotated version of what rails/rspec will generate for controller specs, so I suppose this should be considered "The RSpec Way".

    describe AccountsController do
    
      # Helper method that returns a mocked version of the account model.
      def mock_account(stubs={})
        (@mock_account ||= mock_model(Account).as_null_object).tap do |account|
          account.stub(stubs) unless stubs.empty?
        end
      end
    
      describe "GET index" do
        it "assigns all accounts as @accounts" do
          # Pass a block to stub to specify the return value
          Account.stub(:all) { [mock_account] }
          get :index
          # Assertions are also made against the mock
          assigns(:accounts).should eq([mock_account])
        end
      end
    
      describe "GET show" do
        it "assigns the requested account as @account" do
          Account.stub(:find).with("37") { mock_account }
          get :show, :id => "37"
          assigns(:account).should be(mock_account)
        end
      end
    
      describe "GET new" do
        it "assigns a new account as @account" do
          Account.stub(:new) { mock_account }
          get :new
          assigns(:account).should be(mock_account)
        end
      end
    end