1

我是 capybara & 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 语句。但是测试失败了。原因是“访问根路径”之后,结果(page.html)只包含整个根的静态部分。我不知道为什么。

然后我尝试了没有 rails & rspec 的独立 capybara,它工作正常。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的动态语言,应用广泛,简洁直观,让你一生受用&lt;/p>
    <a class="btn btn-primary" href="/select_courses/buy/2">购买</a>&nbsp;
    <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
4

1 回答 1

0

这是你的整个测试文件吗?

# 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

似乎您的主题未在 rspec 文件中定义。

你应该有subject { page }可能之后require 'spec_helper'

于 2012-07-02T16:52:07.063 回答