如何将块传递给have_selector?
我有以下视图,无法在视图规范中正确指定:
file: "products/index.html.haml"
#products
= render @products
这是我的视图规范:
require 'spec_helper'
describe "products/index.html.haml" do
let(:products) {[mock_model(Product)]}
before do
stub_template "products/product.html.haml" => ""
render
end
it "should render the products" do
rendered.should have_selector(#products) do
rendered.should render_template products
end
end
这里的问题是 have_selector 不接受块,所以有无法断言部分内容是在 #products div 内呈现的。由于 Capybara 匹配器不能在 View 规范中工作,因此您不能在其中任何一个规范中使用。
I have the following view which I can't spec out properly in a view spec:
file: "products/index.html.haml"
#products
= render @products
And this is my view spec:
require 'spec_helper'
describe "products/index.html.haml" do
let(:products) {[mock_model(Product)]}
before do
stub_template "products/product.html.haml" => ""
render
end
it "should render the products" do
rendered.should have_selector(#products) do
rendered.should render_template products
end
end
The problem here is that have_selector does not accept a block so there is no way to assert that the partial is rendered inside the #products div. Since Capybara matchers don't work within View specs you cannot use within either.
See also this issue: https://github.com/rspec/rspec-rails/issues/387
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确答案是 webrat 确实会阻塞,而 capybara 则不会。
在这个问题中,水豚的创建者 Jonas Nicklas 解释了为什么没有添加它,并且他不打算添加它:
https://github.com/jnicklas/capybara/issues/384
有一些使用 Object#tap 将块传递给水豚的 #find 的示例也许曾经有效,但似乎不再有效。
更新:David 确认目前无法使用 rspec/capybara 执行此操作:
https://groups.google.com/forum/?fromgroups=#!topic/rspec/HBfznq4Yd0k
The correct answer is with webrat does take a block, and capybara does not.
Here's the issue in which Jonas Nicklas, the creator of capybara explains why it doesn't and that he's not planning on adding it:
https://github.com/jnicklas/capybara/issues/384
There were some examples using Object#tap to pass blocks to capybara's #find that perhaps once worked, but don't seem to be working any longer.
UPDATE: Confirmation from David that there is no way to do this presently with rspec/capybara:
https://groups.google.com/forum/?fromgroups=#!topic/rspec/HBfznq4Yd0k
我所做的是测试部分呈现的类或 id。 have_selector 确实接受块。
What I do is test for a class or id that the partial renders. And have_selector does accept blocks.