首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Webrat迁移到Capybara...unsuccessfully

从Webrat迁移到Capybara...unsuccessfully
EN

Stack Overflow用户
提问于 2011-05-22 02:44:46
回答 4查看 3.3K关注 0票数 7

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

我正试图让Capybara在一个现有的小型application...and中工作,我没有任何运气。

Gemfile:

代码语言:javascript
复制
  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:

代码语言:javascript
复制
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>"__)而引入了一些通用页面

代码语言:javascript
复制
  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:

代码语言:javascript
复制
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

失败:

代码语言:javascript
复制
  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)>'
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-11-10 04:34:19

只是有同样的问题,发现水豚需要:

代码语言:javascript
复制
response.body.should have_selector("title")

webrat不需要.body

此外,确保您正在呈现视图例如:。

代码语言:javascript
复制
describe PagesController do
  render_views
票数 14
EN

Stack Overflow用户

发布于 2011-11-21 07:59:30

您应该在您的测试文件中包括capybara DSL:

代码语言:javascript
复制
require 'spec_helper'

    describe PagesController do

    include Capybara::DSL

      describe "GET 'about'" do
        it "should be successful" do 
      ...
票数 1
EN

Stack Overflow用户

发布于 2011-05-22 18:03:02

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

代码语言:javascript
复制
page.html.should match(/About/i)

尝试:

代码语言:javascript
复制
page.should match(/About/i)

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6085718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档