开发者

Rails3 + Rspec2: undefined method `authenticate!' for nil:NilClass

开发者 https://www.devze.com 2023-02-23 07:11 出处:网络
All my tests f开发者_JS百科ail when I add before_filter authenticate_user! to my controllers. How do I get by this?

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/

0

精彩评论

暂无评论...
验证码 换一张
取 消