如何将块传递给have_selector?

时间:2011-06-02 05:41:48

标签: ruby-on-rails-3 views rspec2 capybara specs

我有以下视图,我无法在视图规范中正确指出:

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

这里的问题是has_selector不接受一个块,所以没有办法断言部分是在#products div中呈现的。由于Capybara匹配器在View规范中不起作用,因此无法在其中使用。

另请参阅此问题:https://github.com/rspec/rspec-rails/issues/387

2 个答案:

答案 0 :(得分:4)

正确的答案是webrat确实需要阻止,而水豚则没有。

以下是capybara的创造者Jonas Nicklas解释为什么不这样做以及他不打算添加它的问题:

https://github.com/jnicklas/capybara/issues/384

有一些例子使用Object#tap将块传递给capybara的#find,这可能曾经有效,但似乎不再有效了。

更新:David确认目前无法用rspec / capybara执行此操作:

https://groups.google.com/forum/?fromgroups=#!topic/rspec/HBfznq4Yd0k

答案 1 :(得分:0)

我所做的是测试部分呈现的类或id。并且have_selector接受块。

 it "should render the products" do
   rendered.should have_selector('#products') do |products|
     products.should have_select('.product')
   end
   rendered.should render_template 'products'
 end
相关问题