I have a requirement to read a third-party public website. Currently I am able to do this via Nokogiri, but I’m having trouble writing some unit tests using rspec.
I have this HTML <div>:
<div class="user">
<div class="name">User Name</div>
</div>
In my Reader model I have the method name, which reads the name from the HTML:
class SampleReader
def name(html_div)
html_div.css('name')
end
end
In my RSpec test case I pass the above HTML <div> as a string to the name method and I get the following error:
undefined method `css' for #<String:0x007fd8a0b39c98>
I believe it’s because Nokogiri cannot identified the string as HTML, so I would appreciate if someone can help me write the test case. And, if possible, my preferred option is to pass only the <div> string, not the entire HTML page source, to the method.
- I’m using Rails 3.2.9
- Rspec2
- Nokogiri
You’ll need to wrap the HTML string as a Nokogiri document:
Also, don’t forget to upgrade your application to rails 3.2.11.