我目前正在通过Hartl工作。在第5章中,我将Hartl的代码从5.27(下面)列表添加到我的spec / requests / static_pages_spec.rb
require 'spec_helper'
describe "Static pages" do
subject { page }
describe "Home page" do
before { visit root_path }
it { should have_selector('h1', text: 'Sample App') }
it { should have_selector('title', text: full_title('')) }
it { should_not have_selector 'title', text: '| Home' }
end
describe "Help page" do
before { visit help_path }
it { should have_selector('h1', text: 'Help') }
it { should have_selector('title', text: full_title('Help')) }
end
describe "About page" do
before { visit about_path }
it { should have_selector('h1', text: 'About') }
it { should have_selector('title', text: full_title('About Us')) }
end
describe "Contact page" do
before { visit contact_path }
it { should have_selector('h1', text: 'Contact') }
it { should have_selector('title', text: full_title('Contact')) }
end
end
当我运行$ bundle exec rspec spec / requests / static_pages_spec.rb test时,终端返回此错误:
Failures:
1) Static pages Home page
Failure/Error: it { should have_selector('title', text: full_title('')) }
NoMethodError:
undefined method `full_title' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fb4b44504c8>
# ./spec/requests/static_pages_spec.rb:11:in `block (3 levels) in <top (required)>'
2) Static pages Help page
Failure/Error: it { should have_selector('title', text: full_title('Help')) }
NoMethodError:
undefined method `full_title' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2:0x007fb4b46a9008>
# ./spec/requests/static_pages_spec.rb:19:in `block (3 levels) in <top (required)>'
3) Static pages About page
Failure/Error: it { should have_selector('title', text: full_title('About Us')) }
NoMethodError:
undefined method `full_title' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_3:0x007fb4b4430290>
# ./spec/requests/static_pages_spec.rb:26:in `block (3 levels) in <top (required)>'
4) Static pages Contact page
Failure/Error: it { should have_selector('title', text: full_title('Contact')) }
NoMethodError:
undefined method `full_title' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_4:0x007fb4b40e57e8>
# ./spec/requests/static_pages_spec.rb:33:in `block (3 levels) in <top (required)>'
Finished in 0.21306 seconds
9 examples, 4 failures
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:11 # Static pages Home page
rspec ./spec/requests/static_pages_spec.rb:19 # Static pages Help page
rspec ./spec/requests/static_pages_spec.rb:26 # Static pages About page
rspec ./spec/requests/static_pages_spec.rb:33 # Static pages Contact page
有什么想法吗?
答案 0 :(得分:1)
你的spec_helper.rb档案中有这一行吗?
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
你有一个看起来像这样的spec / support / utilities.rb文件吗?
include ApplicationHelper
def sign_in(user)
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
# Sign in when not using Capybara as well.
cookies[:remember_token] = user.remember_token
end
你有app / helpers / application_helper.rb文件,看起来像这样吗?
module ApplicationHelper
# Returns the full title on a per-page basis.
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
end
如果是这样,我认为你的错误应该消失。
答案 1 :(得分:1)
您好我还关注了Michael的Rails 3.2版本教程并得到了相同的错误...尝试使用当前版本的capybara
我将:文字更改为:内容并使其正常工作,所以它看起来像这样:
it { should have_selector('title', content: full_title('')) }
希望它有所帮助,欢呼!
答案 2 :(得分:0)
第5章的练习是
“消除了对清单5.26中的full_title测试助手的需求 为原始帮助器方法编写测试,如清单所示 5.37。 (您必须同时创建spec / helpers目录和
application_helper_spec.rb
文件。)然后将其包含在测试中 代码清单5.38中的代码。“
http://ruby.railstutorial.org/chapters/filling-in-the-layout.html#sec-layout_exercises
这是否意味着我只需要“包含ApplicationHelper”我的utilites.rb
文件,因为spec/helpers/application_helper_spec.rb
现在包含
require 'spec_helper'
describe ApplicationHelper do
describe "full_title" do
it "should include the page title" do
full_title("foo").should =~ /foo/
end
进行此设置后,我在运行RSpec测试时仍然出错。他们只能在full_title
utilites.rb
才能让它通过
答案 3 :(得分:0)
当前格式应如下所示,以便您的测试通过:
it { should have_title(full_title('Help')) }
it { should have_title(full_title('About')) }
it { should have_title(full_title('Contact')) }
答案 4 :(得分:0)
考虑到这篇文章已有两年了,我猜你已经弄明白了。不过,请尝试添加
RSpec.configure do |config|
...
config.include ApplicationHelper
...
end
在spec \ requests \ spec_helper.rb
中