【问题标题】:RSpec fails to find dynamically created :titleRSpec 找不到动态创建的:title
【发布时间】:2013-02-14 06:42:26
【问题描述】:

我正在阅读 railstutorial.org 的书,但我被卡住了。就像标题所说的那样,RSpec 没有找到标题标签。在重构之前,我做了一个检查,它通过了......在底部我只添加了一个页面,但创建了另外两个页面。除去标题,它们都是一样的。

cmd RSpec 输出:

失败的例子:

rspec ./spec/requests/static_pages_spec.rb:40 # 静态页面应该 标题为“关于我们”的 rspec ./spec/requests/static_pages_spec.rb:11 # 静态页面主页 应该有标题'Home' rspec ./spec/requests/static_pages_spec.rb:25 # 静态页面帮助页面 应该有标题“帮助”

宝石文件:

source 'https://rubygems.org'

gem "rails", "~> 3.2.12"

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

group :development, :test do    gem 'sqlite3', '~> 1.3.7'   gem 'rspec-rails', '~> 2.12.2' end


# Gems used only for assets and not required
# in production environments by default. group :assets do   gem 'sass-rails',   '~> 3.2.4'   gem 'coffee-rails', '~> 3.2.2'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes   # gem 'therubyracer', :platforms => :ruby

  gem "uglifier", "~> 1.3.0" end

gem "jquery-rails", "~> 2.2.1"

group :test do  gem "capybara", "~> 2.0.2" end

group :production do    gem "pg", "~> 0.14.1" end

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'

路线:

SampleApp::Application.routes.draw do
  get "static_pages/home"

  get "static_pages/help"

  get "static_pages/about"
end

spec.rb:

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 title 'Home'" do
            visit '/static_pages/home'
            page.should have_selector('title',
                        :text => "Ruby on Rails Tutorial Sample App | Home")
        end
    end

    describe "Help page" do

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

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

    describe "About page" do

        it "should have the h1 'About Us'" do
            visit '/static_pages/about'
            page.should have_selector('h1', :text => 'About Us')
        end
    end

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

application.html.erb:

<!DOCTYPE html>
<html>
<head>
  <title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

home.html.erb:

<% provide(:title, "Home") %>

<h1>Sample App</h1>
<p>
This is the home page for the
<a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
sample application.
</p>

【问题讨论】:

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


    【解决方案1】:

    它不起作用的原因是因为您安装了 Capybara 版本 ~&gt; 2.0.2 而不是本教程使用的 1.1.2

    2.0.0 及以上版本在page 中默认不再显示title 标记中的文本(Hartl 确保在他的 Gemfile:对于可能出现的此类问题)。

    Github issue 中的更多详细信息,解决该问题的方法是在 this StackOverflow Q&A 中(尽管我建议您现在只坚持教程指定的 gem 版本,也许会回到这个作为重构练习)。

    【讨论】:

    • 非常感谢您查看此内容...我最初使用的是教程和故障排除的旧 pdf,导致我升级到 ruby​​gems.org 版本。它解决了我的旧问题,但随后又创建了一个新问题,哈哈。 Capybara 问题确实有道理,现在将在 github 上阅读。
    【解决方案2】:

    我去了网站,复制并粘贴到他的 gemfile 中,现在所有的工作都正常了...奇怪的是我从 RubyGems.org 手动复制了所有的 gem,所以我不确定为什么这些没有工作...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-25
      • 1970-01-01
      • 2014-12-08
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多