0

I have added user authentication to my rails application using this railscast (270#): http://railscasts.com/episodes/270-authentication-in-rails-3-1

The problem is, now two of my functional tests do not pass:

 1) Failure:
test_should_create_user(UsersControllerTest) [/home/tim/fairym/test/functional/users_controller_test.rb:20]:
"User.count" didn't change by 1.
<4> expected but was
<3>.

  2) Error:
test_should_update_user(UsersControllerTest):
NoMethodError: undefined method `stringify_keys' for "980190962":String
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/attribute_assignment.rb:69:in `assign_attributes'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/persistence.rb:215:in `block in update_attributes'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/transactions.rb:208:in `transaction'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/persistence.rb:214:in `update_attributes'
    /home/tim/fairym/app/controllers/users_controller.rb:71:in `block in update'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/mime_responds.rb:270:in `call'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/mime_responds.rb:270:in `retrieve_collector_from_mimes'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/mime_responds.rb:194:in `respond_to'
    /home/tim/fairym/app/controllers/users_controller.rb:70:in `update'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/abstract_controller/base.rb:167:in `process_action'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/rendering.rb:10:in `process_action'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:414:in `_run__2246469619999623246__process_action__2071820246947825486__callbacks'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `__run_callback'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/abstract_controller/callbacks.rb:17:in `process_action'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/rescue.rb:29:in `process_action'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/notifications.rb:123:in `block in instrument'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/notifications.rb:123:in `instrument'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/abstract_controller/base.rb:121:in `process'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/abstract_controller/rendering.rb:45:in `process'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/testing.rb:17:in `process_with_new_base_test'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/test_case.rb:469:in `process'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/test_case.rb:49:in `process'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/test_case.rb:396:in `put'
    /home/tim/fairym/test/functional/users_controller_test.rb:46:in `block in <class:UsersControllerTest>'

Here are the tests:

require 'test_helper'
setup do
    @input_attributes ={
    :email => "[email protected]", 
    :first_name => "sam",
    :last_name => "Sammy",
    :gender => "Male",
    :orientation => "Bi",
    :location => "London",
    :date_joined => 2012 - 10 - 18
    :date_of_birth => 1987 - 10 - 18
    :last_online => DateTime.now.to_date
    :password => "private",
    :password_confirmation => "private"
    :admin => false
    }
test "should update user" do

    put :update, :id => @user.to_param, :user => @input_attributes
    assert_redirected_to users_path
  end
  test "should create user" do
    assert_difference('User.count') do
      post :create, :user => { @input_attributes }
    end

    assert_redirected_to users_path

end

sessions_controller.rb

def create
  user = User.find_by_email(params[:email])
  if user && user.authenticate(params[:password])
    session[:user_id] = user.id
    redirect_to root_url, :notice => "Logged in!"
  else
    flash.now.alert = "Invalid email or password"
    render "new"
  end
end

def destroy
  session[:user_id] = nil
  redirect_to root_url, :notice => "Logged out!"
end

application_controller.rb

private

def current_user
  @current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user

I would like to make those tests pass by adding authentication, how can I do that?

1 Answer 1

1

This might be a long way of solving your problem but in your test instead of doing this

put :update, :id => @user.to_param, :user => @input_attributes

do this

put :update, :id => @user.to_param, :user => { :email => @input_attributes.email, :first_name => @input_attributes.first_name etc...}

Specifying each individual user attribute got rid of the error for me.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.