Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using have_selector against a String object in Rails + RSpec

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.

like image 981
leandroico Avatar asked Oct 02 '22 00:10

leandroico


1 Answers

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

like image 123
Chris Gunther Avatar answered Oct 13 '22 10:10

Chris Gunther