测试 升级到 Capybara2.0, 找不到 visit 方法. 是我火星了么?

southwolf · 2013年01月18日 · 最后由 zhang_soledad 回复于 2013年07月01日 · 3436 次阅读

一直都是用 Capybara 1.x 最近才刚开始看 2.0

直接换上 2.0 之后首先碰到一个错误

NoMethodError:
       undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007ff2f576dac8>

转了一圈才发现 Capybara 2.0 已经把 spec/requests 给挪到 spec/features

http://alindeman.github.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html

https://github.com/rspec/rspec-rails/blob/master/Capybara.md

https://github.com/rspec/rspec-rails/issues/360

论坛里没见有人提过……是我火星了么……

嗯,应该是。不过也没什么,一般来说大的升级(1.x -> 2.0)都应该去看看文档再用的。

这个是刚刚做的改动吧,我之前也遇到了。

改个目录而已。。。

#3 楼 @40hood 可是单纯看错误提示你完全想不到是因为目录改了这种问题……

#4 楼 @southwolf 所以要养成看文档的好习惯 😄

但不知道为什么 rails g integration_test xxxxx 仍然是生成 spec/requests/xxxxx_spec.rb 不知道有没有办法改一下。。。。。。

rspec 2.0 中 page.should have_selector(:title, "Home") 也不能用

#7 楼 @hbin 可以写成page.should have_selector(:title, text: "Home")

#9 楼 @hbin 我刚看了一下我的代码,

it "should have home title" do
  should have_selector('title', text: "Home")
end

测试通过的,我的 rspec-rails 2.12.2 stackflow 里面可能是他的代码问题,不是 rspec 的问题,我看到下面有这么句话和我想法一致。 The <title> tag is present on the page but the text My Title is somewhere else on the page, not within the tag.

@hbin @Vincent178 @neverlandxy_naix Capybara 2.0 现在的策略是 have_selector 只能选择页面中显示的文字。包括display: hidden<title>等 不在页面中显示的标签一律抓不到。

需要改成

page.should have_xpath("//title[contains(.,'Home')]")

9 楼 @Vincent178 发的 StackOverflow 讨论中也有人提到了这个

Github 上的讨论: https://github.com/jnicklas/capybara/issues/844

下面怎么讨论讨论就歪楼了。。 这个问题是在spec_helper.rb中加入一段代码:

config.include Capybara::DSL

#12 楼 @southwolf 楼上不对。

page.should have_title("your_title")   #it's true

14 楼正解 2.1 的新 api have_title

需要 登录 后方可回复, 如果你还没有账号请 注册新账号