加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 从Webrat迁移到Capybara ……失败了

发布时间:2020-12-17 04:22:10 所属栏目:百科 来源:网络整理
导读:希望有人看到我忽略的东西…… 我试图让Capybara在一个现有的小应用程序中工作……而且我没有运气. 的Gemfile: group :development,:test do gem 'rspec-rails' # gem 'webrat' gem 'capybara',:git = 'git://github.com/jnicklas/capybara.git' end ... 两
希望有人看到我忽略的东西……

我试图让Capybara在一个现有的小应用程序中工作……而且我没有运气.

的Gemfile:

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

两个地方的类似规格因各种原因而失败.不知道为什么?

规格/控制器/ 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是“<!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">nn" 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)>'

规格/视图/页/ 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)>'

解决方法

刚遇到同样的问题,发现水豚需要:
response.body.should have_selector("title")

webrat不需要.body

另外,请确保您正在呈现视图,例如:

describe PagesController do
  render_views

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读