0

我已使用此 railscast (270#) 将用户身份验证添加到我的 rails 应用程序: http ://railscasts.com/episodes/270-authentication-in-rails-3-1

问题是,现在我的两个功能测试没有通过:

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

以下是测试:

require 'test_helper'
setup do
    @input_attributes ={
    :email => "jojo@yahoo.com", 
    :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

结尾

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

我想通过添加身份验证使这些测试通过,我该怎么做?

4

1 回答 1

1

这可能是解决您的问题的很长的路要走,但在您的测试中而不是这样做

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

做这个

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

指定每个单独的用户属性为我消除了错误。

于 2013-04-19T00:33:07.883 回答