Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails link_to not found by Capybara

_header.html.erb

<header class="navbar navbar-fixed-top">
  <div class="navbar-inner">
    <div class="container">
      <%= link_to "sample app", root_path, id: "logo" %>
      <nav>
        <ul class="nav pull-right">
          <li><%= link_to "Home", root_path %></li>
          <li><%= link_to "Help", help_path %></li>
          <% if signed_in? %>
            <li><%= link_to "Users", '#' %></li>
            <li id="fat-menu" class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                Account <b class="caret"></b>
              </a>
              <ul class="dropdown-menu">
                <li><%= link_to "Profile", current_user %></li>
                <li><%= link_to "Settings", '#' %></li>
                <li class="divider"></li>
                <li>
                  <%= link_to "Sign out", signout_path, method: "delete" %>
                </li>
              </ul>
            </li>
          <% else %>
            <li><%= link_to "Sign in", signin_path %></li>
          <% end %>
        </ul>
      </nav>
    </div>
  </div>
</header>

user_pages_spec.rb

    describe "with valid information" do
        before do
            fill_in "Name",         with: "Example User"
            fill_in "Email",        with: "[email protected]"
            fill_in "Password",     with: "foobar"
            fill_in "Confirmation", with: "foobar"
        end

        it "should create a user" do
            expect { click_button submit }.to change(User, :count).by(1)
        end

        describe "after saving the user" do
            before { click_button submit }
            let(:user) { User.find_by_email('[email protected]') }

            it { should have_selector('title', text: user.name) }
            it { should have_selector('div.alert.alert-success', text: 'Welcome') }
            it { should have_link('Sign out') }
        end

        describe "followed by signout" do
            before { click_link "Sign out" }
            it { should have_link('Sign in') }
        end
    end

The failure is:

1) User pages signup with valid information followed by signout
Failure/Error: before ( click_link "Sign out" )
Capybara::ElementNotFound:
no link with title, id, or text 'Sign out' found
(eval):2:in 'click_link'
./spec/requests/user_pages_spec.rb:63:in 'block (5 levels) in (top (required))'

Following the create user process manually it appears to work. The link is in a drop down menu if maybe that might have something to do with it? This is at the point in the MHartl rails tutorial where he says all tests should pass.

like image 604
Roger Avatar asked Dec 06 '22 13:12

Roger


2 Answers

The suspect code apparently needs to be nested inside the inside the previous describe

        describe "after saving the user" do
            before { click_button submit }
            let(:user) { User.find_by_email('[email protected]') }

            it { should have_selector('title', text: user.name) }
            it { should have_selector('div.alert.alert-success', text: 'Welcome') }
            it { should have_link('Sign out') }

            describe "followed by signout" do
                before { click_link "Sign out" }
                it { should have_link('Sign in') }
            end
        end
like image 134
Roger Avatar answered Mar 16 '23 02:03

Roger


The before block in the last describe hasn't yet created the user. This is why the user (which doesn't exist yet) isn't currently signed in, thus no "sign out" link.

One possible solution is to click_button 'submit' in the before block. Another is to skip the sign out in the before block altogether, since there is no user signed in at that point.

like image 45
Tanzeeb Khalili Avatar answered Mar 16 '23 02:03

Tanzeeb Khalili