【问题标题】:Capybara undefined method 'has_title?'?Capybara未定义方法'has_title?'?
【发布时间】:2013-05-27 09:27:40
【问题描述】:

我正在阅读 Mike Hartl 的 Rails 教程,in section 4.4 似乎让我将 rspec 请求文件的格式更改为:

page.should have_selector('title', :text => "#{base_title}")

expect(page).to have_title("Ruby on Rails Tutorial Sample App")

我现在遇到两个未定义的方法错误,我使用“.to have_title”和“.to_not have_title”。为了以防万一,我关闭并重新启动了 Webrick、Spork 和 Guard,但它仍然无法正常工作。

水豚版本 1.1.2 Rspec 版本 2.11.1

如果需要任何其他信息,请告诉我。

【问题讨论】:

标签: ruby-on-rails capybara railstutorial.org


【解决方案1】:

显然教程最近发生了变化。

通过谷歌缓存访问页面会显示原始版本(对我来说很好):

require 'spec_helper'

describe "Static pages" do

  describe "Home page" do

    it "should have the h1 'Sample App'" do
      visit '/static_pages/home'
      page.should have_selector('h1', :text => 'Sample App')
    end

    it "should have the base title" do
      visit '/static_pages/home'
      page.should have_selector('title',
                        :text => "Ruby on Rails Tutorial Sample App")
    end

    it "should not have a custom page title" do
      visit '/static_pages/home'
      page.should_not have_selector('title', :text => '| Home')
    end
  end
  .
  .
  .
end

【讨论】:

  • 所以这确实成功了,当我最近重新加载第 4.4 节的页面时,它实际上有你粘贴在这里的更正版本。 Mike Hartl 从昨天开始修好了吗?我觉得我有点疯了......
  • 另外,请注意,static_pages_spec.rb 文件顶部的代码在describe "Static pages" dodescribe "Home page" do 之间缺少let(:base_title) { "Ruby on Rails Tutorial Sample App" }
【解决方案2】:

可能是您的版本的问题。通过https://github.com/jnicklas/capybara/issues/863

expect(first('title').native.text).to eq "my title"

【讨论】:

  • 我很困惑——我没有使用“第一种”方法......另外,我使用 Capybara v. 1.1.2 和 2.1.0 进行了测试,并且具有相同的结果。
  • 试试这个页面。应该有_xpath("//title", :text => "My Title")
【解决方案3】:

我遇到了这个问题,我最终意识到我已经切换到遵循 Rails 版本 4 的代码,而不是 3.2。

要测试您使用的 Rails 版本,请输入命令

rails -v

然后在教程页面上,使用右侧的导航器选择正确的 rails 版本。看起来很简单,但希望它能为遇到此问题的其他人减轻痛苦。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-08
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    相关资源
    最近更新 更多