RSpec 2.4, named/restful routes falling apart

Guys,

This could be me, but I thought I’d check

For some reason, when I spec controllers, I run into this problem
(intermittently it would seem). Right now, I have a spec that fails
predictably.

I’m using

  • Rspec 2.4.0
  • Rspec-rails 2.4.1
  • Rails 3
  • Mocha 0.9.10

I have the following defined in my routes file

resources :users

And a controller:

class UsersController < ApplicationController
before_filter :load_user

def show
redirect_to(user_path(@user))
end

private
def load_user
@user = User.find(params[:id])
end
end

and a spec

describe UsersController do
before(:each) do
@user = Factory.build(:user, :id => ‘12341234’)
User.expects(:find).returns(@user)
get “show”, :id => @user.id
end
subject { response }
it { should redirect_to(user_path(@user)) }
end

The spec fails with the following

Failures:

  1. UsersController
    Failure/Error: get “show”, :id => @user.id
    ActionController::RoutingError:
    No route matches {:controller=>“users”, :action=>“show”,
    :id=>#<User
    _id: 12341234, encrypted_password:
    “$2a$10$1gbrD8IZSo7LUYE5l5w1B.AYSu6zTs6lzWw.ZPhRKXCNT88xtjZfy”,
    last_sign_in_ip: nil, confirmation_sent_at: nil, last_sign_in_at: nil,
    sign_in_count: 0, password_salt: “$2a$10$1gbrD8IZSo7LUYE5l5w1B.”,
    setup_employer_profile: nil, last_name: “Treutel”, current_sign_in_ip:
    nil,
    reset_password_token: nil, remember_token: nil, current_sign_in_at: nil,
    confirmation_token: “12341234XXX12341234”, remember_created_at: nil,
    first_name: “Carolyn”, confirmed_at: nil, email: "
    [email protected]">}

    ./app/controllers/users_controller.rb:17:in `show’

    ./spec/controllers/users_controller_spec.rb:50

Could somebody explain what’s going on???

It would appear that for some reason, my controllers don’t know a thing
about routing. Either that or something’s wacky. The annoying thing
about
this is that it works perfectly fine when you use a browser to test.

Cheers,
Jason

On Jan 12, 2011, at 11:39 PM, Jason N. wrote:

  • Mocha 0.9.10
    def show

Could somebody explain what’s going on???

It would appear that for some reason, my controllers don’t know a thing about
routing. Either that or something’s wacky. The annoying thing about this is that
it works perfectly fine when you use a browser to test.

What happens if you write the spec as a stock rails functional test?

$ rails generate test_unit:controller Users show

in test/functionals/users_controller_test.rb

require “test_helper”
class UsersControllerTest < ActionController::TestCase
test “show redirects to user path” do
@user = Factory.build(:user, :id => ‘12341234’)
User.expects(:find).returns(@user)
get “show”, :id => @user.id
assert_redirected_to user_path(@user)
end
end

Hi,

On Thu, Jan 13, 2011 at 13:39, Jason N. [email protected] wrote:

end
private
def load_user
@user = User.find(params[:id])
end
end

Perhaps I’m missing something here, but doesn’t this
UsersController#show redirect to itself the way this code is written?

Mike