I am new to capybara & rspec and i wrote a simple test(under spec/requests) to test the my root path:
# 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
The root page contains both static and dynamic content which indeed contains the above xpath statement by firefinder verification. But the test failed. The reason was that after “visit root_path”, the result(page.html) only contained the static part of the whole root. I don’t know why.
I then try standalone capybara without rails & rspec and it worked correctly.
The 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
root page:
<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>
And it should looked like:
<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>
But the result(not including the header and footer) was as follows:
<div class="row">
<div class="span3 courses-category-panel">
<h2>课程分类</h2>
</div>
**<div class="span9 courses">
<ul class="thumbnails"></ul>
</div>**
</div>
We could see that the following dynamic parts are NOT generated:
<%= render partial: 'shared/courses_category', object: @big_categories, as: 'courses_big_categories' %>
<% @courses.each do |course| %>
<%= render(partial: 'shared/course', object: course) %>
<% end %>
Could anyone see this problem or help it?
Add some more info:
root_path is matched to Welcome#index which is defined as follows:
def index
@big_categories = BigCategory.all
@courses = Course.all
end
Is this your entire testing file?
It seems like your subject is not defined in the rspec file.
You should have
subject { page }probably afterrequire 'spec_helper'.