私はカピバラとrspecを初めて使用し、ルートパスをテストするための簡単なテスト(spec/requestsの下)を作成しました:
# encoding: utf-8
require 'spec_helper'
describe "select a course" do
before { visit root_path }
it "should render main page well" do
puts page.html
page.should have_xpath("//ul[@class='thumbnails']/li[1]")
end
end
ルート ページには静的コンテンツと動的コンテンツの両方が含まれており、firefinder 検証による上記の xpath ステートメントが実際に含まれています。しかし、テストは失敗しました。その理由は、「root_path にアクセス」した後、結果 (page.html) にルート全体の静的部分しか含まれていなかったためです。どうしてか分かりません。
次に、レールとrspecなしでスタンドアロンのカピバラを試してみましたが、正しく機能しました。spec_helper.rb:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'capybara/rails'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
end
ルートページ:
<div class="row">
<%= render partial: 'shared/courses_category', object: @big_categories, as: 'courses_big_categories' %>
<div class="span9 courses">
<ul class="thumbnails">
<% @courses.each do |course| %>
<%= render(partial: 'shared/course', object: course) %>
<% end %>
</ul>
</div>
</div>
そして、次のようになります。
<ul class="thumbnails">
<li class="span3">
<div class="thumbnail">
<a href="/courses/2"><img src="http://placehold.it/260x180" alt="掌握ruby"></a>
<div class="caption">
<h5>掌握ruby</h5>
<p class="course-summary">够fashin够cool的动态语言,应用广泛,简洁直观,让你一生受用</p>
<a class="btn btn-primary" href="/select_courses/buy/2">购买</a>
<a class="btn" href="/select_courses/store/2">收藏</a>
<span class="course-price">¥200</span>
</div>
</div>
しかし、結果(ヘッダーとフッターを含まない)は次のとおりです。
<div class="row">
<div class="span3 courses-category-panel">
<h2>课程分类</h2>
</div>
**<div class="span9 courses">
<ul class="thumbnails"></ul>
</div>**
</div>
次の動的パーツが生成されていないことがわかります。
<%= render partial: 'shared/courses_category', object: @big_categories, as: 'courses_big_categories' %>
<% @courses.each do |course| %>
<%= render(partial: 'shared/course', object: course) %>
<% end %>
誰かがこの問題を見たり助けたりできますか?
さらに情報を追加します。root_path は、次のように定義されている Welcome#index に一致します。
def index
@big_categories = BigCategory.all
@courses = Course.all
end