I'm writing a spec upon a file inside lib/ directory. And one of my methods in there returns a HTML string, but then I try something like:
expect(subject).to have_selector("p:eq(1)") do |first_p|
And it gives me the following error:
Failure/Error: expect(subject).to have_selector("p:eq(1)") do |first_p|
NoMethodError:
undefined method `has_selector?' for #<String:0x007f93798a8338>
How can I work around this? Should I wrap the string object with some another specific object? I've tried with Nokogiri::HTML.fragment() but it didn't work out neither. I suppose I should wrap it with some webrat's object or something like that, but I don't know how to get there. Thanks in advance.
Assuming you're using Capybara to get the has_selector?
method, you'll want to wrap your result in Capybara.string(...)
to get access to the has_selector?
method.
So your test should go from:
expect(subject).to have_selector("p:eq(1)")
To:
result = Capybara.string(subject)
expect(result).to have_selector("p:eq(1)")
Documentation: http://rdoc.info/gems/capybara/Capybara#string-class_method
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With