Nokogiri with Rspec

霸气de小男生 提交于 2019-12-12 19:02:56

问题


Is there a way to use Nokogiri in Rspec?

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

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

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

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


回答1:


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

page = Nokogiri::HTML(response.body)

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)


来源:https://stackoverflow.com/questions/24592436/nokogiri-with-rspec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!