Forum: RSpec Passing user id to spec | error

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mano a. (Guest)
on 2009-05-15 18:46
(Received via mailing list)
-----Controller --------------
def update
    @user = User.find_by_id( user.id )
    if request.post?

      unless params[ :user ].nil?

        if @user.update_attributes( params[ :user ] )
          redirect_to :action => :show

        end
      else
        render :status => 400,
               :text => 'No user parameters
end

 ------- spec -------------
describe UserProfilesController do

  before(:each) do
    @user = mock_model(User)
    controller.stub!(:requires_user).and_return @user
    controller.stub!(:user).and_return @user
    User.stub!(:find_by_id).and_return(@user)
    @user.stub!(:update_attributes).and_return(true)
  end

it "should find User and return object" do
  User.should_receive(:find_by_id).with("1").and_return(@user)
   @user.should_receive(:update_attributes).and_return(true)
  post :update, :id =>"1", :user => {}
end
end

------- Error --------------------------------
<User(id: integer, created_on: datetime, updated_on: datetime,
destroyed_on:
dat
etime, accessed_on: datetime, email_address: string, name_first: string,
name_la
st: string, mailing_address_id: integer, billing_address_id: integer,
name_displ
ay: string, picture_id: integer, url: string, country: string,
freelance:
boolea
n, statement: string, hostname_active: boolean, hostname: string,
promotional_ma
ilings: boolean, sales_commission_rate: float, referral_commission_rate:
float,
employee_lock: boolean) (class)> expected :find_by_id with ("1") but
received it
 with ([1001])

How shall I pass the id to the spec. Please help overcome this issue

--
View this message in context:
http://www.nabble.com/Passing-user-id-to-spec-%7C-...
Sent from the rspec-users mailing list archive at Nabble.com.
David C. (Guest)
on 2009-05-15 19:19
(Received via mailing list)
On Fri, May 15, 2009 at 1:02 AM, manoah <removed_email_address@domain.invalid> 
wrote:
>
>  -----Controller --------------
> def update
>    @user = User.find_by_id( user.id )

This uses user.id, not params[:id]

>               :text => 'No user parameters
> end
>
>  ------- spec -------------
> describe UserProfilesController do
>
>  before(:each) do
>    @user = mock_model(User)

The @user gets defined with an ID of 1001 (in this case). Try:

  @user = mock_model(User, :id => 1)

HTH,
David
Mano a. (Guest)
on 2009-05-16 07:45
(Received via mailing list)
manoah wrote:
>
>     @user = mock_model(User)
> end
> boolea
>
---------------------------------------------------------
Now I am getting the below error
> expected :find_by_id with ("1") but received it  with ([1])

How shall I pass
Mano a. (Guest)
on 2009-05-16 09:44
(Received via mailing list)
manoah wrote:
>
>     @user = mock_model(User)
> end
> boolea
>
Thank you David.
The example passes when we pass the id as string
@user = mock_model(User, :id=> "1")






--
View this message in context:
http://www.nabble.com/Passing-user-id-to-spec-%7C-...
Sent from the rspec-users mailing list archive at Nabble.com.
This topic is locked and can not be replied to.