raza.sayed
raza.sayed

Reputation: 549

Rspec error for failing test different than the error seen in browser

Im using RSpec v3.1.0 and Capybara v2.3.0 in my rails app.I have the following in my spec/features/user_creates_todo_spec.rb.

require 'rails_helper'

feature 'User creates todo' do 
    scenario 'successfully' do
        visit root_path

        click_on 'Add a new todo'
        fill_in 'Title', with: 'Buy milk'
        click_on 'Submit'

        expect(page).to have_css '.todos li', text: 'Buy milk'
    end
end

Then in my app/views/todos/index.html.erb, i added the following:

<%= link_to "Add a new todo", new_todo_path %>

When i run rspec spec/features/user_creates_todo_spec.rb,the test fails with an error Capybara::ElementNotFound: Unable to find link or button "Add a new todo"

However, im expecting the error to be: Undefined local variable or method 'new_todo_path', since i already have the link defined in my view . And the strange thing is that when i run the rails server and navigate to the root path from the browser i see the undefined local variable or method 'new_todo_path' error.Also, i see the undefined path error when i just run rake from the root of my app. So, why is it that when i run the spec file individually using rspec i see a different error even though i have the link added to the page ?

Thanks

Upvotes: 0

Views: 135

Answers (2)

zetetic
zetetic

Reputation: 47548

Don't forget that Capybara is opening a browser session, which runs in a new process, and navigating to a path, just as a user would. Your Rails app may indeed by raising an exception, but Capybara is only looking at the response in the browser -- it doesn't know what is happening in the process where the Rails server runs. So it dutifully visits the url, gets the 500 error page, looks for the link and (of course) fails to find it.

Upvotes: 1

Aventuris
Aventuris

Reputation: 630

That is because the link is not added to the page. new_todo_path is not defined which likely means that you haven't added the proper routes for it in your routes.rb file.

Upvotes: 0

Related Questions