如何将块传递给have_selector?

发布于 2024-11-12 03:49:44 字数 761 浏览 6 评论 0原文

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

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 规范中工作,因此您不能在其中任何一个规范中使用。

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈年往事 2024-11-19 03:49:44

正确答案是 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

清眉祭 2024-11-19 03:49:44

我所做的是测试部分呈现的类或 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

What I do is test for a class or id that the partial renders. And have_selector does accept blocks.

 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文