在Michael Hartl的Ruby on Rails教程的第9章中,我遇到了RSpec测试的问题,因为他们没有看到我在文件utilities.rb中定义的sign_in方法。我被卡住了,我检查了一切,看起来是正确的。我看到类似的帖子,其中包含ApplicationHelper的行帮助了某人,但我已经拥有它,而且我对同一文件中的“full_title”方法也没有任何问题。
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] = remember_token
user.update_attribute(:remember_token, User.encrypt(remember_token))
else
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
end
和失败的文件user_pages_spec.rb:
require 'rails_helper'
include ApplicationHelper
...
...
...
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
...
end
答案 0 :(得分:3)
如果您在使用该方法时遇到困难,可以尝试将其直接包含在您的规范中。
require 'rails_helper'
...
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
....
private
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.encrypt(remember_token))
else
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
如果可行,请继续使用帮助方法为您的规范创建一个新模块,然后将其包含在spec_helper / rails_helper中