Question

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.

Was it helpful?

Solution 2

I didn't feel very satisfied about using the type: :view solution that I discovered earlier in this thread, despite it's a very valid one. The thing is that my spec for a file inside lib/ directory shouldn't be actually considered as a view spec.

So I did some further research over the internet and found out one that seems a better approach to me.

So considering the original example in this thread. You should just add the following line inside your describe block:

include Webrat::Matchers

So the final example would look like:

describe MyApp::UI::MyDecoratorClass do
  include Webrat::Matchers
  # ...
end

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top