[rspec-users] [newbie]: How to test for operations inside a block?

Carmine Moleti lists at ruby-forum.com
Mon Sep 22 12:07:41 EDT 2008


Hi to everyone,

I'm just trying to wrap my mind around BDD and RSpec. I've gone through
"The rails' way" and RSpec webcasts series, and still don't have a clear
vision of RSpec. Is there anything else I could read/watch?

Back in topic...
Now I'm writing specs for a "create" action.
The action is written like this:

  def create
    authenticate_with_open_id(params[:identity_url], :required => [
:nickname, :email ], :optional => :fullname) do |result, identity_url,
registration|
      if result.successful?
        @user = User.new
        @user.identity_url = identity_url
        @user.roles << Role.find_by_name('operator')
        @user.user_type = 'operator'
        assign_registration_attributes!(registration)
        @user.save(false)
        redirect_to users_path
      else
        redirect_to new_user_path
      end
    end
  end

How am I supposed to write a spec to test the code shown above?

I tried with:

   it "should check identity_url via OpenID authentication" do
     result = mock('Object')
     result.should_receive(:successful?).and_return(:true)
     @controller.should_receive(:authenticate_with_open_id).with(@identity_url,
:required => [ :nickname, :email ], :optional =>
:fullname).and_return(result)
     do_verb
     result.should be_successful
   end

   it "should create a new user using OpenID authentication" do
     @controller.stub!(:authenticate_with_open_id)
     @user = new_user # thanks to fixture_replacement2
     User.stub!(:new).and_return(@user)
     @user.should_receive(:save).with(false).and_return(true)
     do_verb
   end

The first test pass successfully while the second throws havocs on me.
It complains on the "@user.should_receive(:save..." part.
It also complained when I wrote
"User.should_receive(:new).and_return(@user)", so I changed
it to "User.stub!(:new)...". But I don't fully understand why I had to
do so.

Thanks in advance for your help,
Carmine
-- 
Posted via http://www.ruby-forum.com/.


More information about the rspec-users mailing list