使用水豚和rails3进行Rspec视图测试
我真的很喜欢 RSpec 能够分离控制器和视图测试的方式,但在让水豚匹配器在视图测试中工作时遇到一些问题。我基本上想要实现的是这样的:
describe "some page" do
it "should render with lots of stuff" do
assign ..
render
rendered.should have_button ('Any button') #or any capybara matcher, really
end
end
我在网上看到了一些帖子,展示了如何配置水豚和rails3以顺利地与黄瓜或rspec控制器测试一起工作,但这并不是我真正想要的 - 也就是说,测试尽可能最低级别的意见。
另外,如果有另一种方法可以做到这一点(不需要大量自定义代码,因为我知道我可以编写一些匹配器,使用 nokogiri 或任何合适的工具从渲染中提取给定的选择器),那也很棒 - 不需要使用水豚。
I really like the way RSpec is able to separate controller and view tests but have some problems with getting capybara matchers to work in a view test. What i basically try to achieve is sth like this:
describe "some page" do
it "should render with lots of stuff" do
assign ..
render
rendered.should have_button ('Any button') #or any capybara matcher, really
end
end
I've seen some posts on the net showing how to configure capybara and rails3 to work smoothly with cucumber or rspec controller tests, but this is not really what I want - that is, testing the views at the lowest level possible.
Also if there's another way to do this (not requiring lots of custom code, couse I know i could write some matchers that extract given selectors from rendered using nokogiri or whatever tool suitable) that'd be great too - using capybara is not a requirement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
现在可以选择在测试控制器(以及视图)时使用 Capybara 匹配器(没有 Webrat 包袱)。我以这种方式使用它:
完整代码:
参考文献:
There is now an option to use Capybara matchers (without Webrat baggage) when testing controllers (and views too). I'm using it this way:
Full code:
References:
Capybara 目前不支持视图规范(有计划在未来使其工作)。最简单的答案是只需将
gem 'webrat'
添加到 Gemfile 中即可。您可能没有have_button
,但您可以使用have_selector
、have_tag
和类似的工具。顺便说一句:据我所知,capybara 和 webrat 可以在一个项目中共存。
Capybara currently does not work with view specs (there are plans to make it work in the future). The simplest answer is to just add
gem 'webrat'
to the Gemfile and you're basically set. You might not havehave_button
but you'll havehave_selector
,have_tag
and similar available.Btw: as far as I know capybara and webrat can co-exist in one project.
比 Pawel 的答案稍微简单一些,但要点是一样的;以下适用于 Rails 3.1.0、rspec 2.6.0、capybara 1.1.1:
Slightly simpler than Pawel's answer, but the gist is the same; the following works for me with rails 3.1.0, rspec 2.6.0, capybara 1.1.1:
你不能在
rendered
上调用水豚的方法,那只是一个字符串。您可以使用 Capybara 的 string 方法 来包装rendered
在水豚节点中。然后,您可以在该节点上调用 Capybara 的方法:有关更多信息,请查看这篇文章:
http://www.tamingthemindmonkey.com/2011/11/07/capybara-matchers-and-scoping-in-view-specs
You can't call capybara's methods on
rendered
, that's just a string. You can use Capybara's string method though to wraprendered
in a Capybara node. Then, you can call Capybara's methods on that node:For more information, check out this post:
http://www.tamingthemindmonkey.com/2011/11/07/capybara-matchers-and-scoping-in-view-specs
在此页面底部的“Webrat 和 Capybara”部分中,rspec 视图规范似乎不支持 Capybara
http://relishapp.com/rspec/rspec-rails
At the bottom of this page, in the "Webrat and Capybara" section, it looks like Capybara is unsupported for rspec view specs
http://relishapp.com/rspec/rspec-rails
更新这个旧问题,因为自从添加了大多数其他答案以来,事情已经发生了变化:
Capybara 现在确实支持视图规范(开箱即用),这记录在 水豚的主分支。
引用文档:
最后,视图规范支持 Capybara 匹配器:
对这些匹配器的支持似乎已在早期版本中添加,而无需额外的
let(:page)
样式代码。 (它在水豚 2.4.4 和 2.2 中对我有用)。另请注意,仅支持有限的匹配器子集;但是,您可以通过使用 Capybara.string 获得更多功能;前任:
Updating this old question as things have changed since most of the other answers were added:
Capybara now does support view specs (out of the box) and this is documented on Capybara's master branch.
To quote the docs:
Finally, Capybara matchers are supported in view specs:
Support for these without additional
let(:page)
style code appears to have been added in an earlier version. (It's working for me in capybara 2.4.4 and 2.2).Note also that only a limited subset of matchers are supported; however you can gain more functionality by using Capybara.string; ex:
您还可以使用水豚语法
You can also use capybara syntax