水豚rspec组合无法一起工作,挂了2天 [英] Capybara & rspec combination can't work together, hanging on it for 2 days

查看:60
本文介绍了水豚rspec组合无法一起工作,挂了2天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是水豚的新来者。 rspec和我编写了一个简单的测试(根据spec / requests)以测试我的根路径:

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

根页面既包含静态内容,也包含动态内容,这些内容确实包含上述​​通过firefinder验证的xpath语句。但是测试失败了。原因是在访问root_path之后,结果(page.html)仅包含整个根的静态部分。我不知道为什么

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.

然后我尝试不带滑轨的独立水豚& rspec,它正常工作。
spec_helper.rb:

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  

根页面:

<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>&nbsp;
    <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?

添加更多信息:
root_path匹配到Welcome#index,其定义如下:

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?

# 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文件中定义。

It seems like your subject is not defined in the rspec file.

需要'spec_helper'之后,您应该有主题{页面}

这篇关于水豚rspec组合无法一起工作,挂了2天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆