Rails教程第9.2.2章 - 授权测试失败

时间:2014-03-09 17:37:43

标签: ruby-on-rails authentication rspec ruby-on-rails-4

我正在关注Rails教程并遇到障碍。在第9.2.2节。 http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users#sec-requiring_the_right_user

........................................................FFF...........

Failures:

  1) Authentication authorization as wrong user submitting a GET request to the Users#edit action 
     Failure/Error: before { sign_in user, no_capybara: true }
     ArgumentError:
       wrong number of arguments (2 for 1)
     # ./spec/support/utilities.rb:37:in `sign_in'
     # ./spec/requests/authentication_pages_spec.rb:71:in `block (4 levels) in <top (required)>'

  2) Authentication authorization as wrong user submitting a GET request to the Users#edit action 
     Failure/Error: before { sign_in user, no_capybara: true }
     ArgumentError:
       wrong number of arguments (2 for 1)
     # ./spec/support/utilities.rb:37:in `sign_in'
     # ./spec/requests/authentication_pages_spec.rb:71:in `block (4 levels) in <top (required)>'

  3) Authentication authorization as wrong user submitting a PATCH request to the Users#update action 
     Failure/Error: before { sign_in user, no_capybara: true }
     ArgumentError:
       wrong number of arguments (2 for 1)
     # ./spec/support/utilities.rb:37:in `sign_in'
     # ./spec/requests/authentication_pages_spec.rb:71:in `block (4 levels) in <top (required)>'

Finished in 3.1 seconds
70 examples, 3 failures

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:75 # Authentication authorization as wrong user submitting a GET request to the Users#edit action 
rspec ./spec/requests/authentication_pages_spec.rb:76 # Authentication authorization as wrong user submitting a GET request to the Users#edit action 
rspec ./spec/requests/authentication_pages_spec.rb:81 # Authentication authorization as wrong user submitting a PATCH request to the Users#update action 

Randomized with seed 40912

我无法弄清楚出了什么问题。

源代码可用,所以我不会混淆这个页面: https://bitbucket.org/davidhughes85/brand_new/src

3 个答案:

答案 0 :(得分:0)

我猜测,虽然代码的链接已经过时 - exercise sign_in API看起来像这样:

def sign_in(user, options={})
  if options[:no_capybara]
    # Sign in when not using Capybara.
    remember_token = User.new_remember_token
    cookies[:remember_token] = remember_token
    user.update_attribute(:remember_token, User.hash(remember_token))
  else
    visit signin_path
    fill_in "Email",    with: user.email
    fill_in "Password", with: user.password
    click_button "Sign in"
  end
end

错误消息显示wrong number of arguments (2 for 1)中有sign_in。并且因为接受两个参数的唯一一行(假设before { sign_in user, no_capybara: true })是user.update_attribute,并且在练习页面中也有user.update_attributes接收一个参数,我的钱就在你身上错字:

def sign_in(user, options={})
  if options[:no_capybara]
    # Sign in when not using Capybara.
    remember_token = User.new_remember_token
    cookies[:remember_token] = remember_token
    user.update_attributes(:remember_token, User.hash(remember_token)) # <== TYPO
  else
    visit signin_path
    fill_in "Email",    with: user.email
    fill_in "Password", with: user.password
    click_button "Sign in"
  end
end

答案 1 :(得分:0)

您的环境不了解您的功能,可能是您在运行某些测试运行时编写的(不知道是否真实),尝试使用
{{1重置测试环境这应该解决你的问题,希望它可以帮助有同样问题的人。

答案 2 :(得分:0)

我遇到了同样的问题。我花了一段时间才找到它,但问题出在utilities.rb文件中。查看方法def sign_in(user, options={ })

问题在于您可能写过:

def sign_in(user, options={ })
  if options[:no_capybara]
    # Sign in when not using Capybara
    remember_token = User.new_remember_token
    cookies[:remember_token] = remember_token
    user.update_attributes(:remember_token, User.digest(remember_token))
  else
    visit signin_path
    fill_in "Email",     with: user.email
    fill_in "Password",  with: user.password
    click_button "Sign in"
  end
end

你应该写:

def sign_in(user, options={ })
  if options[:no_capybara]
    # Sign in when not using Capybara
    remember_token = User.new_remember_token
    cookies[:remember_token] = remember_token
    user.update_attribute(:remember_token, User.digest(remember_token))
  else
    visit signin_path
    fill_in "Email",     with: user.email
    fill_in "Password",  with: user.password
    click_button "Sign in"
  end
end

区别在于您已在if区块user.update_attributes(...)中撰写。你应该写user.update_attribute(...)。这是一个意外多元化方法调用的问题。