dcush
dcush

Reputation: 1

Rails 4 fixtures aren't generating IDs (Hartl tutorial 9.6.2)

I'm creating an app based on the Hartl tutorial, and I'm stuck on the second exercise:

Write an integration test for all the layout links, including the proper behavior for logged-in and non-logged-in users. Hint: Add to the test in Listing 5.25 using the log_in_as helper.

The code I have in my test is

require 'test_helper'

class SiteLayoutTest < ActionDispatch::IntegrationTest

def setup
   @user = users(:test)
end

test 'layout links' do
   get root_path
   assert_template 'static_pages/home'
   assert_select "a[href=?]", root_path, count: 2
   assert_select "a[href=?]", help_path
   assert_select "a[href=?]", about_path
   assert_select "a[href=?]", contact_path
   assert_select "a[href=?]", login_path
   get signup_path
   assert_select "title", full_title("Signup")
   log_in_as @user
   get root_path
   assert_select "a[href=?]", users_path, text: "Users"
   assert_select "a[href=?]", user_path
   assert_select "a[href=?]", edit_user_path(@user)
   assert_select "a[href=?]", logout_path

end end

Here's the fixture I'm using:

test:
 name: Test Name
 derby_name: Test Derby Name
 email: [email protected]
 password_digest: <%= User.digest('password') %>
 admin: true

And here's the error I keep getting when I run a test:

ERROR["test_layout_links", SiteLayoutTest, 2015-07-07 21:08:39 +0000]                                                              
 test_layout_links#SiteLayoutTest (1436303319.87s)                                                                                 
ActionController::UrlGenerationError:         ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=
>"users"} missing required keys: [:id]                                                                                             
            test/integration/site_layout_test.rb:22:in `block in <class:SiteLayoutTest>'                                           
        test/integration/site_layout_test.rb:22:in `block in <class:SiteLayoutTest>' 

It looks to me like my fixtures aren't generating IDs for some reason, even though when I run the test manually I can access the site navigation URLs just fine and when I run rails console --sandbox and create a new User object it generates an ID. I've tried searching for an answer to this but with no luck (I've been stuck on this for days!).

I'm using a Codio IDE with Rails 4.2.0 and Ruby 2.1.5 with Minitest as the test suite.

EDIT: For reference, here's my log_in_as helper:

# Logs in a test user
def log_in_as(user, options = {})
    password = options[:password] || 'password'
    remember_me = options[:remember_me] || '1'
    if integration_test?
        post login_path, session: { email: user.email,
            password: password, 
            remember_me: remember_me }
    else
        session[:user_id] = user.id
    end
end

Just in case that helps out anyone in the future :)

Thanks, Baldrick, for solving it below!

Upvotes: 0

Views: 137

Answers (1)

Baldrick
Baldrick

Reputation: 24350

The important part of the error message is ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"users"} missing required keys: [:id]. The method user_path needs a user (it is the path to show a given user), so it should be

assert_select "a[href=?]", user_path(@user)

Upvotes: 1

Related Questions