带有 Rspec 的 Nokogiri [英] Nokogiri with Rspec

查看:43
本文介绍了带有 Rspec 的 Nokogiri的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 Rspec 中使用 Nokogiri?

Is there a way to use Nokogiri in Rspec?

特别是我试图从控制器操作中获取响应并将其转换为 Nokogiri 对象 page 并运行 Nokogiri 特定的解析,如下所示:

Particularly I'm trying to take the response from a controller action and convert that into a Nokogiri object page and run Nokogiri-specific parsing like the one below:

page.search('input[name="some_name"]').size.should == 1

我在哪里包含 Nokogiri - 那会在 spec_helper.rb 中吗?

Where do I include Nokogiri - would that be in spec_helper.rb?

如何将 ActionController::TestResponse 转换为 Nokogiri 对象?

How do I convert the ActionController::TestResponse into a Nokogiri object?

或者是否可以使用普通的 Rspec 语法来运行上述类型的断言?

Or is it possible to run the above kind of assertion by using plain Rspec syntax?

推荐答案

如果你没有使用 capybara,按照@sevenseacat 的建议,您可以执行以下操作:

If you're not using capybara, as suggested by @sevenseacat, you can do something like:

page = Nokogiri::HTML(response.body)

然后使用Nokogiri 搜索方法.但是 capybara 确实是一个更好的方法,因为它包含了查询模块,让我们可以做诸如

And then use the Nokogiri search methods. But capybara is really a better way to go because it includes the query module that let's do do things like

expect(page).to have_css('input[name=some_name]', count:1)

这篇关于带有 Rspec 的 Nokogiri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆