ruby-on-rails - 从 Webrat 迁移到 Capybara...失败

标签 ruby-on-rails rspec capybara

希望有人能看到我忽略的东西......

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

gem 文件:

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

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

规范/ Controller /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)>'

规范/ View /页面/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)>'

最佳答案

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

response.body.should have_selector("title")

webrat 不需要 .body

另外,请确保您正在渲染 View ,例如:。
describe PagesController do
  render_views

关于ruby-on-rails - 从 Webrat 迁移到 Capybara...失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6085718/

相关文章:

ruby-on-rails - AuthLogic 表单给我不正确的验证错误——为什么?

ruby-on-rails - 在实时数据库上运行迁移是否安全?

ruby-on-rails - 将同名参数传递给 Rspec POST 请求以创建数组

ruby-on-rails-3 - rspec 测试 ruby​​ on rails api 由看门人保护

ruby - 下拉菜单在应该被选中的时候没有被选中……为什么?

ruby-on-rails - ember-cli/rails capybara/rspec 测试

ruby-on-rails-3 - rspec 忽略skip_before_filter?

ruby-on-rails - 在 Rails 3 中更改 Cookie 值的问题

ruby-on-rails - 对 Rails 测试完全感到困惑。哪些工具适用于哪些工作?

ruby-on-rails - ActiveAdmin 表单不保存嵌套对象