Search code examples
ruby-on-rails-3.1rspec-rails

Rspec Routing Error, :id=>#User id:nil


I've got link_to "My Account", edit_user_path(current_user) working in the browser however Rspec is giving me the following error

  1) Can edit user details
     Failure/Error: current_path.should eq(edit_user_path(@user))
     ActionController::RoutingError:
       No route matches {:action=>"edit", :controller=>"users", :id=>#<User id: nil, username: "test_user_6", email: "[email protected]", crypted_password: "cf10df3a34c12b54fd3d99c11395d43ec8f663a48ab8b32917d...", password_salt: "7cZsZhedN2z5Pxbrcl", persistence_token: "59f5a50ac21151f1bc875b5a8b1d14896fb2010dd14dab9e7cd...", perishable_token: nil, created_at: nil, updated_at: nil>}

The spec

  it "can edit user details" do
    integration_sign_up(@user)
    click_link("My Account")
    current_path.should eq(edit_user_path(@user))
  end

Running tail -f log/test.log returns 200 so I'm guessing it's an Rspec issue?

Started GET "/users/565/edit" for 127.0.0.1 at 2012-01-05 17:03:30 +1100
  Processing by UsersController#edit as HTML
  Parameters: {"id"=>"565"}
Rendered layouts/_head.html.haml (1.6ms)
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."persistence_token" = 'ec26b6f501ff97a0f6b467c3caabb7e93a1cf97905194d030b465d3163d846e2fad9dc0ccbfc371e16b920c57634e843f544d7015eb9dbafda5711e6ec7b9018' LIMIT 1
Rendered layouts/_navigation.html.haml (2.9ms)
Rendered layouts/_flashes.html.haml (0.1ms)
Rendered layouts/_footer.html.haml (1.1ms)
Completed 200 OK in 11ms (Views: 10.8ms | ActiveRecord: 0.4ms)
   (0.2ms)  ROLLBACK

Testing in the browser is successful.

Do I need an Rspec work around or am I doing something wrong?

Given the Rspec error has id=>#User id:nil I'm guessing it's Rspec?


Solution

  • The error message is telling you exactly what the problem is. The @user object doesn't have an id, so Rails can't generate a route. Make sure @user has an id by either stubbing it or saving the record.