Rails3 + Rspec2: undefined method `authenticate!' for nil:NilClass
All my tests f开发者_JS百科ail when I add before_filter authenticate_user! to my controllers. How do I get by this?
Thanks
You have to login a user in your tests. I don't know the authentication method you use, but i will make a wild guess. If it's devise, create a spec/support/controller_macros.rb and insert :
module ControllerMacros
def login_user
before(:each) do
@request.env["devise.mapping"] = :user
@user = Factory.create(:user)
sign_in @user
end
end
end
I use factory girl to create a factory, but you can do it however you like. Then, in your tests add it like :
describe AlliancesController do
login_user
describe "GET 'show' without an id" do
......
put include Devise::TestHelpers to your spec http://pupeno.com/2010/09/26/undefined-method-authenticate/
精彩评论