【问题标题】:Migrating from Webrat to Capybara...unsuccessfully从 Webrat 迁移到 Capybara...不成功
【发布时间】:2011-12-17 21:24:13
【问题描述】:

希望有人能看到我忽略的内容...

我正试图让 Capybara 在一个现有的小型应用程序中工作……但我没有任何运气。

宝石文件:

  group :development, :test do
    gem 'rspec-rails'
    # gem 'webrat'
    gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git'
  end
  ...

由于不同的原因,两个地方的类似规范都失败了。不知道为什么?

spec/controllers/pages_controller_spec.rb:

require 'spec_helper'

describe PagesController do

  describe "GET 'about'" do
    it "should be successful" do
      # get 'about'                         #worked w/ webrat
      # response.should be_success          #worked w/ webrat
      visit pages_about_path
      # page.should have_content('About Us') 
      page.html.should match(/About/i)
    end

    it "should have title" do
      # get 'about'                         #webrat
      # response.should have_selector("title", :content => "About Us") #webrat
      visit pages_about_path                
      page.should have_selector("title")    
    end
  end  
end

失败: (可能会拉入一些通用页面,因为浏览器中的文档类型是 "<!DOCTYPE html>"

  1) PagesController GET 'about' should be successful
     Failure/Error: page.html.should match(/About/i)
       expected "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n\n" to match /About/i
     # ./spec/controllers/pages_controller_spec.rb:13:in `block (3 levels) in <top (required)>'

  2) PagesController GET 'about' should have the right title
     Failure/Error: page.should have_selector("title") 
       expected css "title" to return something
     # ./spec/controllers/pages_controller_spec.rb:20:in `block (3 levels) in <top (required)>'

spec/views/pages/about.html.haml_spec.rb:

require 'spec_helper'

describe "pages/about.html.haml" do
  it "renders attributes in <p>" do
    # render #webrat
    # rendered.should match(/About/) #webrat
    visit pages_about_path
    page.should have_content("About Us")
  end

  it "should have the right heading" do  
    # render #webrat
    # rendered.should have_selector("h2", :content => "About Us") #webrat
    visit pages_about_path
    page.should have_selector("h2")
  end
end

失败:

  1) pages/about.html.haml renders attributes in <p>
     Failure/Error: visit pages_about_path
     NoMethodError:
       undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000101dc2970>
     # ./spec/views/pages/about.html.haml_spec.rb:8:in `block (2 levels) in <top (required)>'

  2) pages/about.html.haml should have the right heading
     Failure/Error: visit pages_about_path
     NoMethodError:
       undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x000001034b1d98>
     # ./spec/views/pages/about.html.haml_spec.rb:16:in `block (2 levels) in <top (required)>'

【问题讨论】:

  • 请将 nigelr(或您认为应得的人)标记为可能有此问题的其他人的可接受答案,并作为普遍礼貌。

标签: ruby-on-rails rspec capybara


【解决方案1】:

刚遇到同样的问题,发现水豚需要:

response.body.should have_selector("title")

webrat 不需要 .body

另外,请确保您正在渲染视图,例如:。

describe PagesController do
  render_views

【讨论】:

  • 这就是答案。另外,如果您来自 railstutorial.org,我相信您还需要将 :content => "whatever" 更改为 :title => "whatever"。否则不管内容如何,​​它都会通过。
  • 我认为 yakiimo 的意思是 have_selector("title", :content =&gt; "Home") 变为 have_selector("title", :text =&gt; "Home")。我还发现have_selector("a", :href =&gt; "/users?page=2", :content =&gt; "Next") 更改为have_link("Next", :href =&gt; "/users?page=2")。我发现这篇博文中的前后示例很有帮助:Migrating from Webrat to Capybara
【解决方案2】:

你应该在你的测试文件中包含 capybara DSL:

require 'spec_helper'

    describe PagesController do

    include Capybara::DSL

      describe "GET 'about'" do
        it "should be successful" do 
      ...

【讨论】:

    【解决方案3】:

    这不会解决你所有的问题,而是:

    page.html.should match(/About/i)
    

    尝试:

    page.should match(/About/i)
    

    (你甚至可能不需要页面)。

    另外,请检查您是否已将 Capybara 设置为在 env.rb 中使用 CSS 查询(默认为 XPath)。

    【讨论】:

    • 删除“html”。没用。此外,Capybara 的阅读声明说:“Capybara 不会尝试猜测您将提供哪种选择器,并且会默认始终使用 CSS。如果您想使用 XPath,您将需要做...”看到错误“undefined method `visit'”让我觉得 Capybara 没有正确安装或引用...但不知道如何确认!?!
    • 嗯,Cucumber 生成的 env.rb 显示“Capybara 默认使用 XPath 选择器,而不是 Webrat 的默认 CSS3”。但是,是的,听起来它没有拿起水豚。您是否尝试过通过 Bundler 运行?捆绑执行黄瓜
    • 不使用 Cucumber...真的不想...这可能是问题吗?
    【解决方案4】:

    确保您的 spec_helper.rb 文件顶部包含以下内容:

    ENV["RAILS_ENV"] ||= 'test'
    require File.expand_path("../../config/environment", __FILE__)
    require 'rspec/rails'
    require 'rspec/autorun'
    require 'capybara/rspec'
    

    我遇到了和你类似的问题(即undefined method 'visit'),弹出这些行解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-27
      • 2014-01-27
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-19
      • 1970-01-01
      相关资源
      最近更新 更多