0

ページのタイトルをrspecで確認したい。クロムでは期待される結果が表示されますが、rspecはタイトルが空であると主張しています(クロムビューソースでもOKです)。ここにいくつかのコード:

application.html.rb

<!DOCTYPE html>
<html>
<head>
  <title>site | <%= 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><%= yield(:title) %></h1>
<article>
  <div><a href="#">Cameras</a></div>
  <div><a href="#">TVs</a></div>
  <div><a href="#">Laptops</a></div>
</article>

home_page_controller.rb

class HomePageController < ApplicationController
  def home
  end
end

ルート.rb

Reviewsite::Application.routes.draw do
   get "home_page/home"

home_pages_spec.rb

require 'spec_helper'
feature "HomePages" do

  before { visit '/home_page/home'}

  scenario do
    expect(page).to have_selector('h1', text: "Home")
  end

  scenario do
    expect(page).to have_selector('title', text: "site | Home")
  end

end

そしてエラー

Failures:

  1) HomePages 
     Failure/Error: expect(page).to have_selector('title', text: "site | Home")
     Capybara::ExpectationNotMet:
       expected to find css "title" with text "site | Home" but there were no matches. Also found "", which matched the selector but not all filters.
     # ./spec/features/home_pages_spec.rb:12:in `block (2 levels) in <top (required)>'

Finished in 2.52 seconds
2 examples, 1 failure

編集: ここに私のgemfileがあります

source 'https://rubygems.org'

gem 'rails', '3.2.12'
gem 'bootstrap-sass', '2.3.0.1'
gem 'bcrypt-ruby', '3.0.1'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'
gem 'jquery-rails', '2.2.1'
gem 'mysql2'
#gem 'execjs'
gem 'therubyracer', :platforms => :ruby

# Dev and test gems
group :development, :test do
  gem 'rspec-rails', '2.13.0'
  gem 'guard-rspec', '2.4.1'
  gem 'guard-spork', '1.4.2'
  gem 'spork', '0.9.2'
end

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '3.2.6'
  gem 'coffee-rails', '3.2.2'
  gem 'uglifier', '1.3.0'
end

#only test gems
group :test do
  gem 'capybara', '2.0.2'
  gem 'factory_girl_rails', '4.2.1'
  gem 'cucumber-rails', '1.3.0', :require => false
  gem 'database_cleaner', '0.9.1'
  # gem 'launchy', '2.1.0'
  # gem 'rb-fsevent', '0.9.1', :require => false
  # gem 'growl', '1.0.3'
end

group :production do
  gem 'pg', '0.12.2'
end
4

2 に答える 2

0

問題はセッションに関するものだと思います。itカピバラは、それぞれまたはscenarioあなたのケースでセッションを期限切れにします。

したがってvisit page、最初のシナリオでは消費され、2 番目のシナリオでは食べるものがありません。

次のいずれかを試すことができます。

  1. その部分を削除しbefore { visit '/home_page/home'}、すべてのシナリオでページにアクセスしてください。
  2. この 2 つのシナリオを 1 つに結合します。しかし、それでも、visitその中に入れることをお勧めします。
于 2013-02-27T17:33:23.397 に答える
0

それ以外の

expect(page).to have_selector('title', text: "site | Home")

に変更しました

expect(page.html).to have_selector('title', text: "site | Home")

そしてそれはうまくいきました、私はそれを再確認しました。ページだけでは機能しない理由を誰かが理解している場合は、共有してください。

于 2013-02-28T16:34:15.967 に答える