问题
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