[rspec-users] Newbie question

Andy Goundry andy at adveho.net
Tue Dec 4 10:41:40 EST 2007


Good day all :-)

I am getting into RSpec and am a little confused by an aspect of mocks /
mock_models in controller tests. I've gone through the online docs and the
PeepCode movies and have them slightly contradictory on this matter. I hope
you can clarify. Many thanks in advance :-)

I'm writing a system that uses the restful authentication plugin and am
writing a test for the following controller create method:

class AccountsController < ApplicationController
  def create
    @account = Account.new(params[:account])
    if @account.save!
      self.current_account = @account
      redirect_back_or_default('/')
      flash[:notice] = "Thanks for signing up!"
    end
    rescue ActiveRecord::RecordInvalid
      render :action => 'new'
  end
...
end

I've created the following spec test to simply ensure that the Account model
receives a call to :new, but the spec is failing as it also receives a call
to :save! and it isn't expecting it. I am using "@account =
mock_model(Account)". If i replace this with "@account = mock('account',
:null_object => true)" the test passes, but i'm not sure that's really what
i want. I'm pretty sure i need to us mock_model in this instance.

The code is below.

Also, are my use of comment blocks within the spec against the RSpec *way*.
I appreciate that this isn't using fixtures and i might use them later. I
also appreciate that i can write modules to contain these methods, but i
can't quite see the point in muddying things. Comments most welcome.

Thanks

describe AccountsController, "allows users to create only valid accounts" do

  #######
  ### Reusable methods
  #######

    def do_post(params = nil)
      post :create, :account => params
    end

    def valid_account_form_details
      { :password => "password", :password_confirmation => "password",
:login => "new_login", :email => "test at test.com" }
    end

  #######
  ### Before block
  #######

    before do
      @account = mock_model(Account)
      Account.stub!(:new).and_return(@account)
    end

  #######
  ### It Statements
  #######

    it "should display the new account template when the user visits
/accounts/new" do
      get 'new'
      response.should render_template(:new)
    end

    it "should tell the Account model to create a new account on form POST"
do
      Account.should_receive(:new).with(:anything).and_return(@account)
      do_post
    end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071204/151ae095/attachment.html 


More information about the rspec-users mailing list