gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 06:10:18 26 4
gpt4 key购买 nike

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

我试图让 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com