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

Carmine Moleti lists at ruby-forum.com
Mon Sep 22 17:34:28 EDT 2008


So, after spending a little time trying to work with "and_yield", I 
strongly believe that:

- Either I haven't understood what "and_yield" does
   - And this is more than possible

- Or "and_yield" is not the toy I should be playing with.

What I'm trying to accomplish is to writing tests for the following code 
(refactored after great advices from ///ark):

  def create
    authenticate_with_open_id(params[:identity_url], :required => [ 
:nickname, :email ], :optional => :fullname) do |result, identity_url, 
registration|
      if result.successful?
        @user = User.create_with_role(:operator, identity_url, 
registration)
        redirect_to users_path
      else
        redirect_to new_user_path
      end
    end
  end

I believe I should test that "authenticate_with_open_id" gets called on 
@controller, and that
"create_with_role" gets called on "User" class.

So I wrote this:

   def do_verb(options = { :identity_url => @identity_url })
     post :create, options
   end

   it "should create a new user using OpenID authentication" do
     @user = create_operator_user
     @controller.should_receive(:authenticate_with_open_id)
     User.should_receive(:create_with_role).and_return(@user)
     do_verb
     response.should redirect_to(users_path)
   end

The test works up to the point where I set expectations on 
":authenticate_with_open_id", then
it fails with:
Mock 'Class' expected :create_with_role with (any args) once, but 
received it 0 times

I tried this:

     @controller.should_receive(:authenticate_with_open_id).and_yield do 
|a,b,c|
      User.should_receive(:create_with_role).and_return(@user)
    end

and this:

     @controller.should_receive(:authenticate_with_open_id).and_yield
      User.should_receive(:create_with_role).and_return(@user)

Both leading to the following error:

Mock 'UsersController' yielded || to block with arity of 3

Where am I doing wrong?

Thanks in advance for your help,
Carmine that really can't wrap his mind around RSpec :/
-- 
Posted via http://www.ruby-forum.com/.


More information about the rspec-users mailing list