从 Webrat 迁移到 Capybara...不成功

发布于 2024-11-09 02:53:40 字数 3020 浏览 2 评论 0原文

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

我正在尝试让水豚在现有的小型应用程序中工作...但我没有任何运气。

Gemfile:

  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

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

  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)>'

Hoping someone might see what I've overlooked...

I'm trying to get Capybara working in a small existing application...and I'm not having any luck.

Gemfile:

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

Similar specs in two places are failing for different reasons. Not sure why?

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

Failures:
(may be pulling in some generic page as doctype in browser is "<!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

Failures:

  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)>'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

祁梦 2024-11-16 02:53:40

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

response.body.should have_selector("title")

webrat 也不需要 .body

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

describe PagesController do
  render_views

Just had the same issue and found that capybara needs:

response.body.should have_selector("title")

webrat does not need the .body

also, make sure you are rendering views eg:.

describe PagesController do
  render_views
柠栀 2024-11-16 02:53:40

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

require 'spec_helper'

    describe PagesController do

    include Capybara::DSL

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

You should include capybara DSL in your test file :

require 'spec_helper'

    describe PagesController do

    include Capybara::DSL

      describe "GET 'about'" do
        it "should be successful" do 
      ...
花开半夏魅人心 2024-11-16 02:53:40

这不会解决您的所有问题,但可以改为:

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

尝试:(

page.should match(/About/i)

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

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

This won't fix all your problems, but Instead of:

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

try:

page.should match(/About/i)

(you might not even need page).

Also, check that you have Capybara set to use CSS queries in env.rb (it defaults to XPath).

慈悲佛祖 2024-11-16 02:53:40

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

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

我遇到了与您类似的问题(即,未定义的方法“visit”)并弹出这些行解决了问题。

Ensure that your spec_helper.rb file has the following at the top:

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

I had a similar issue as yours (i.e., undefined method 'visit') and popping those lines solved the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文