Rspec用capybara和rails3查看测试 [英] Rspec view testing with capybara and rails3
问题描述
我真的很喜欢RSpec能够分离控制器和视图测试的方式,但有一些问题,使得capybara匹配器在视图测试中工作。我基本上试图实现的是sth这样:
描述一些页面do
it很多东西do
assign ..
render
rendered.should have_button('Any button')#or any capybara matcher,really
end
end
我看到一些网上的帖子显示如何配置capybara和rails3工作顺利与黄瓜或rspec控制器测试,但这不是我想要的 - 也就是测试最低级别的视图。
此外,如果有另一种方式来做这个很多自定义代码,couse我知道我可以写一些匹配器提取给定的选择器从渲染使用nokogiri或任何适合的工具),这将是伟大的 - 使用capybara不是一个要求。
描述GlobalizeTranslationsController do
render_views
let :page){Capybara :: Node :: Simple.new(@ response.body)}
描述PUT:updatedo
before do
put:update
end
it显示闪光通知do
page.should have_selector('p.notice')
end
end
end
完整代码:
$ b b
参考文献:
- http://robots.thoughtbot.com/post/8087279685/use-capybara-on-any-html-fragment-or-page
- http://cakebaker.42dh.com/2010/09/19/cucumber-switching-从-webrat到capybara /
- http://trevorturk.com/2010/12/22/using-capybara-without-cucumber-in-rails-3
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.
There is now an option to use Capybara matchers (without Webrat baggage) when testing controllers (and views too). I'm using it this way:
describe GlobalizeTranslationsController do
render_views
let(:page) { Capybara::Node::Simple.new(@response.body) }
describe "PUT :update" do
before do
put :update
end
it "displays a flash notice" do
page.should have_selector('p.notice')
end
end
end
Full code:
References:
- http://robots.thoughtbot.com/post/8087279685/use-capybara-on-any-html-fragment-or-page
- http://cakebaker.42dh.com/2010/09/19/cucumber-switching-from-webrat-to-capybara/
- http://trevorturk.com/2010/12/22/using-capybara-without-cucumber-in-rails-3
这篇关于Rspec用capybara和rails3查看测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!