失败/错误:登录用户未定义方法`sign_in' [英] Failure/Error: sign_in user undefined method `sign_in'
问题描述
我被困在 Rails 教程的第 9 章 - 更具体地说是在第 9.1 节的末尾.我的问题类似于这个线程中的问题,但是那里的解决方案对我不起作用.
I'm stuck at Chapter 9 in the Rails tutorial - more specifically at the end of section 9.1. My problem is similar to the one in this thread but the solution there didn't work for me.
这是我的 user_pages_spec.rb:
Here is my user_pages_spec.rb:
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup page" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
describe "profile page" do
let (:user) {FactoryGirl.create(:user)}
before { visit user_path(user) }
it { should have_content(user.name) }
it { should have_title(user.name) }
end
describe "signup" do
before { visit signup_path }
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user@example.com"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
end
end
describe "edit" do
let(:user) { FactoryGirl.create(:user)}
before do
sign_in user
visit edit_user_path(user)
end
describe "page" do
it { should have_content("Update your profile")}
it { should have_title("Edit user")}
it { should have_link('change', href:'http://gravatar.com/emails')}
end
describe "with invalid information" do
before { click_button "Save changes"}
it { should have_content('error') }
end
describe "with valid information" do
let(:new_name) { "New Name"}
let(:new_email) {new@example.com}
before do
fill_in "Name", with: new_name
fill_in "Email", with: new_email
fill_in "Password", with: user.password
fill_in "Confirm Password", with: user.password
click_button "Save changes"
end
it {should have_title(new_name)}
it {should have_selector('div.alert.alert-success')}
it {should have_link('Sign out', href: signout_path)}
specify {expect(user.reload.name).to eq new_name}
specify {expect(user.reload.email).to eq new_email}
end
end
end
错误信息如下:
bundle exec rspec spec/
.............................................FFFFFFFFF
Failures:
1) User pages edit page
Failure/Error: sign_in user
NoMethodError:
undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_4::Nested_1:0x007faa37859d80>
# ./spec/requests/user_pages_spec.rb:49:in `block (3 levels) in <top (required)>'
这是我的规范/support/utilities.rb:
And here is my spec/support/utilities.rb:
def full_title(page_title)
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
"#{base_title} | #{page_title}"
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]
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
有什么建议吗?
推荐答案
我在尝试安装 rcov 时确实运行了 bundle install(Rubymine 抱怨缺少它).安装失败,因为 rcov 不适用于我的 Rails 版本.没关系.
I did run bundle install while I was trying to install rcov (Rubymine was complaining about its lack). The installation failed as rcov is not available for my version of rails. That's fine.
真正奇怪的是,之后我重新运行了测试,一切正常.没有任何错误消息的痕迹.我是一个 rails noob 但这有点丰富 - 失败的捆绑安装不会改变任何东西.我观察到的错误消息没有任何原因,现在它无缘无故地消失了.
The really bizarre thing is that afterwards I re-ran the tests and everything worked. There was no trace of the error message. I am a rails noob but this is a bit rich - a failed bundle install shall not change anything. There was no reason for the error message I observed and now it disappeared without any reason.
==我意识到问题是 rails 似乎没有在测试之间清空缓存(在我看来,这是一个可怕的错误).默认情况下,它无法重新读取文件,因此可能会忽略已发生的更改.我在这里提供了更多详细信息:Rails 教程:未定义的方法
== I realised the problem is that rails does not seem to empty the cache between tests (which is, in my opinion, a scary bug). By default it fails to re-read the files and thus may ignore changes that have occurred. I put more details here: Rails tutorial: undefined method
这篇关于失败/错误:登录用户未定义方法`sign_in'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!