1

我升级到 Rails 3.2 和 Capybara 2.1,我的 700 个测试中有大约 30% 失败了。一堆失败的测试是这样的:

require 'spec_helper'

describe BooksController do
  render_views
  let(:page)  { Capybara::Node::Simple.new(@response.body) }

      describe "new" do

        it "should get new" do
          get :new, :author_id => author_token, :publish_action => "Publish"
          response.should be_success
          page.should have_selector "h1", text:"Transition" 
        end
   end
 end

失败的测试输出是:

BooksController should get new content
     Failure/Error: page.should have_selector 'h1', text:'Preview'
     Capybara::ExpectationNotMet:
       expected to find css "h1" with text "Preview" but there were no matches
     # ./spec/controllers/books_controller_spec.rb:46:in `block (5 levels) in <top (required)>'

当我使用 print page.html、save_and_open_page 等时,我得到一个空白或没有输出。但是很多其他的测试都通过了。这似乎与检查 h1 标签有关。

4

1 回答 1

0

查看升级到 Capybara 2部分下的 rspec-rails 文档。我承认这个信息有些隐藏,我花了一段时间才找到。

基本上,为了使用 Capybara DSL(页面和访问),您必须将您的规范移动到spec/features目录中。所以你只能page & visit在验收测试中使用。控制器规格中没有更多页面和访问。(get|post|put|delete|head/response.body)控制器规格中只允许使用机架测试 DSL 。

建议这样做,但有一种方法可以让您的规格保持原样:

RSpec.configure do |c|
  c.include Capybara::DSL, :example_group => {
    :file_path => "spec/requests"
  }
end

文档指出,如果您走这条路,那么您将覆盖预期的行为并且您正在承担风险。

希望这能让你走上正确的道路

于 2013-05-15T03:15:40.413 回答