1

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

文件:“产品/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 规范中不起作用,因此您也不能在其中使用。

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

4

2 回答 2

4

正确的答案是 webrat 确实需要一个块,而 capybara 不需要。

这是 capybara 的创建者 Jonas Nicklas 解释为什么不这样做并且他不打算添加它的问题:

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

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

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

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

于 2012-12-14T04:02:36.383 回答
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
于 2011-06-08T06:16:35.750 回答