我在Rails教程中第9章 - 更具体地说,在第9.1节末尾。我的问题类似于this thread中的问题,但那里的解决方案对我不起作用。
这是我的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)>'
这是我的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
有什么建议吗?
答案 0 :(得分:0)
我在尝试安装rcov时确实运行了bundle install(Rubymine抱怨它缺乏)。安装失败,因为rcov不适用于我的rails版本。没关系。
真奇怪的是,事后我重新进行了测试,一切正常。没有错误消息的痕迹。我是一个rails noob,但这有点丰富 - 一个失败的捆绑安装不会改变任何东西。没有理由我发现错误信息,现在它没有任何理由消失了。
==编辑:我意识到问题是rails似乎没有清空测试之间的缓存(在我看来,这是一个可怕的bug)。默认情况下,它无法重新读取文件,因此可能会忽略已发生的更改。我在这里提供了更多详细信息:Rails tutorial: undefined method