我是新来的水豚和rspec的,我写了一个简单的测试(根据规格/请求)来测试我的根路径:
# 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
根页面包含这的确包含火力发现者验证上述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匹配到其欢迎的定义如下#指数:
def index
@big_categories = BigCategory.all
@courses = Course.all
end