Search code examples
ruby-on-railsrspeccapybararspec-rails

Capybara undefined method 'current_scope'


I have the following code

scenario "it should not appears submenus' links if it doesn't has any" do
  expect(page.find('li.main-category', text: "Stargazing")).not_to have_tag('ul.nested')
end

and when I run the test I get this error

 NoMethodError:
 undefined method `current_scope' for #<Capybara::Node::Element:0x0055d1ab86e430>

I am trying to test this code

<ul class="menu vertical">
   <li class="main-category"><a href="#">Solar System</a>
     <ul class="nested vertical menu">
       <li class="subcategory"><a href="#">Planets</a></li>
     </ul>
   </li>
   <li class="main-category"><a href="#">Stargazing</a></li>
</ul>

The li tag with text 'Stargazing' should not contain a ul tag

Any ideas?


Solution

  • I'm not sure what library the have_tag matcher you're using is coming from, but that's not a Capybara matcher and is not compatible with Capybara. You want

    expect(page.find('li.main-category', text: "Stargazing")).not_to have_css('ul.nested')
    

    or have_css('ul') if you are trying to make sure there are no ul elements at all, and not just no ul elements with a class of nested.

    Also make sure your Capybara is updated.