【问题标题】:Hartl Chapter 5. Section 5.27Hartl 第 5 章。第 5.27 节
【发布时间】:2015-03-16 23:46:26
【问题描述】:

我目前正在通过 Hartl 工作。在第 5 章中,我将清单 5.27(如下)中的 Hartl 代码添加到我的 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 测试时,终端返回此错误:

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

有什么想法吗?

【问题讨论】:

  • 您是否按照 Hartl 所说的那样使用 full_title 方法制作了 spec/support/utilities.rb

标签: ruby-on-rails rspec


【解决方案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

如果是这样,我认为你的错误应该消失。

【讨论】:

  • 在这三个中,spec/support/utilities.rb 是我唯一需要更改的一个,但是问题仍然存在。
  • 好的,解决了我的错误。 spec_helper.rb 不在支持目录中(将其放在规范目录中)。谢谢和道歉。
  • 好的,我还有一些想法。假设您使用的是 ruby​​ 1.9.2,请将以下内容添加到(至少)测试组中的 Gemfile 中: gem 'ruby-debug19' 此命令将添加一个 gem,允许您在代码中插入断点。然后,打开utilities.rb 文件,在'include ApplicationHelper' 行之后,插入'debugger' 行。然后,从命令行运行您的测试。将会发生的是,测试将在调试器语句处暂停执行。当他们暂停时,输入命令“self.class”并按回车键。如果连接正确,您应该会看到“对象”返回。
  • 让我知道该语句返回的内容。如果您使用的是 1.9.2 以外的 ruby​​ 版本,则需要使用 ruby​​-debug19 以外的 gem,pry 是我非常喜欢的 gem,提供了更多功能,所以我将修改说明那。如果您看到 self.class 返回 Object 以外的其他内容,则可能是其中一个 require 语句放错了位置。
【解决方案2】:

您好,我也在关注 Michael 的 Rails 3.2 版本教程并得到相同的错误...尝试使用您当前版本的水豚

我将 :text 更改为 :content 并让它工作,所以它看起来像这样:

it { should have_selector('title', content: full_title('')) }

希望对你有帮助,干杯!

【讨论】:

    【解决方案3】:

    第 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 测试时仍然遇到错误。他们唯一能让我通过的方法是在utilites.rb中定义full_title

    【讨论】:

      【解决方案4】:

      当前格式应如下所示,以便您的测试通过:

      it { should have_title(full_title('Help')) }
      
      it { should have_title(full_title('About')) }
      
      it { should have_title(full_title('Contact')) }
      

      【讨论】:

        【解决方案5】:

        考虑到这篇文章已经有两年了,我想你已经明白了。不过,请尝试添加

        RSpec.configure do |config|
        ...
        config.include ApplicationHelper
        ...
        end
        

        在规范\请求\spec_helper.rb 内

        【讨论】: