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

Mark Wilden mark at mwilden.com
Mon Sep 22 12:24:19 EDT 2008

On Mon, Sep 22, 2008 at 9:07 AM, Carmine Moleti <lists at ruby-forum.com>wrote:

>  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?

The first thing I would do would be to refactor this code into the User
model. The controller shouldn't be concerned with assigning the user_type,
for example.

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.

Others may have more insight, but I would want to know what the specific
complaints were.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080922/801293d9/attachment.html>

More information about the rspec-users mailing list