user938363
user938363

Reputation: 10350

Rspec failed with update in controller

Here is the rspec error for update in customers controller:

 5) CustomersController GET customer page 'update' should be successful
     Failure/Error: put 'update', id => 1, :customer => {:name => 'name changed'}
       <Customer(id: integer, name: string, short_name: string, contact: string, address: string, country: string, phone: string, fax: string, email: string, cell:
string, sales_id: integer, test_eng_id: integer, safety_eng_id: integer, web: string, category1_id: integer, category2_id: integer, active: boolean, biz_status: str
ing, input_by_id: integer, quality_system: string, employee_num: string, revenue: string, note: text, user_id: integer, created_at: datetime, updated_at: datetime)
(class)> received :find with unexpected arguments
         expected: (1)
              got: ("1")
     # ./app/controllers/customers_controller.rb:44:in `update'
     # ./spec/controllers/customers_controller_spec.rb:41:in `block (3 levels) in <top (required)>'

Here is the rspec code:

it "'update' should be successful" do

  customer = mock_model(Customer) 
  Customer.should_receive(:find).with(1).and_return(customer)
  customer.stub(:update_attributes).and_return(true)    
  put 'update', :id => 1, :customer => {:name => 'name changed'}
  response.status.should == 302 #redirect()
end

Here is the update in controller:

  def update
    @customer = Customer.find(params[:id])
    if @customer.update_attributes(params[:customer], :as => :roles_new_update)
      if @customer.changed
        @message = 'The following info have been changed\n' + @customer.changes.to_s
        @subject ='Customer info was changed BY' + session[:user_name]
        notify_all_in_sales_eng(@message,@subject)
      end  

      redirect_to session[('page'+session[:page_step].to_s).to_sym], :notice => 'Customer was updated successfaully!'
    else
      render 'edit', :notice => 'Customer was not updated!'
    end
  end

Any thoughts? Thanks.

Upvotes: 2

Views: 1484

Answers (2)

David Sulc
David Sulc

Reputation: 25994

The values posted (and accessed via the params hash) are strings. The easiest way to correct your test is to have Customer.should_receive(:find).with("1").and_return(customer).

Notice we now have "1" (i.e. a String) as the expected argument instead of 1 (a FixNum).

Upvotes: 4

Gazler
Gazler

Reputation: 84150

All params are passed through as strings and I believe the find method is doing an implicit conversation to an integer. Either make it explicit using to_i in the controller or change your spec to expect the string "1".

Upvotes: 1

Related Questions