thank_you
thank_you

Reputation: 11107

Can't convert symbol into integer in rspec testing

I'm writing a simple test checking to see if my controller works, however my tests keep returning

3) PasswordResetsController for checking update succeeds 
 Failure/Error: put :update, id: @user.password_reset_token
 TypeError:
   can't convert Symbol into Integer
 # ./app/controllers/password_resets_controller.rb:23:in `update'
 # ./spec/controllers/password_resets_controller_spec.rb:52:in `block (3 levels) in 
 <top (required)>'

My tests are as follows...

describe PasswordResetsController do

before do
  @user = FactoryGirl.create( :user )
end

context "for checking update succeeds" do
  before do
    @user.password_reset_token = "gB5KGF-3hC4RNHCK5niz9A"
    @user.password_reset_sent_at = Time.zone.now
    @user.save!(validate: false) 
    put :update, id: @user.password_reset_token
  end


  it { should assign_to(:user) }      
  it { should respond_with(:redirect) }
  it { should redirect_to(signin_path) }
  it { should set_the_flash.to("Password has been reset.")}

end

end

Here is a link to my Model https://github.com/thejourneydude/template/blob/master/app/models/user.rb

EDIT: It appears that the problem is originating from my controller. On my local server, I'm returned with

TypeError in PasswordResetsController#update

can't convert Symbol into String

Rails.root: /home/jason/ror/template
Application Trace | Framework Trace | Full Trace

app/controllers/password_resets_controller.rb:19:in `update'

My update action in my controller code is..

def update
  @user = User.find_by_password_reset_token!(params[:id])
  if @user.password_reset_sent_at && @user.password_reset_sent_at > 2.hours.ago
    redirect_to new_password_reset_path, flash: { error: "Password reset has expired" }
  elsif @user.update_attributes(params[:user])
    redirect_to root_url, flash: { success: "Password has been reset." }
  else 
    render :edit, flash: { error: "Hmm.. something went wrong. Let's try that again." }
  end
end

What's wrong with my code and how do I fix it?

Upvotes: 1

Views: 1409

Answers (4)

ysbecca
ysbecca

Reputation: 131

You are not passing the :user parameter, so perhaps this line would give you an error:

elsif @user.update_attributes(params[:user])

Try doing:

before do
  put :update, :id => @user.password_reset_token, :user => valid_user_attributes
end

Set valid_user_attributes to the update you want, for example { 'email' => '[email protected]' }

Upvotes: 1

Sean Hill
Sean Hill

Reputation: 15056

What happens if you change this line:

redirect_to new_password_reset_path, flash[:error] = "Password reset has expired"

to:

redirect_to new_password_reset_path, flash: { error: "Password reset has expired" }

?

My guess is that you should not be able to call flash[:error] = in the redirect_to since assignment in Ruby returns the value being assigned. redirect_to requires a hash as the second parameter, but it is being called as a string.

Upvotes: 1

Andy Lindeman
Andy Lindeman

Reputation: 12165

Are you actually using describe "tests controller"? If so, you might try changing it to describe TestsController or describe UsersController (whichever is appropriate) so that RSpec knows what controller is under test.

Otherwise, can you post the full backtrace from the exception message?

Upvotes: 0

Rodrigo Oliveira
Rodrigo Oliveira

Reputation: 913

Try change id: to :id, like this:

before do
   put :update, :id => @user.password_reset_token
 end

Upvotes: 0

Related Questions