Ruby on Rails functional test with Devise authentication

前端 未结 3 1820
深忆病人
深忆病人 2021-02-06 02:57

I\'m searching for a solution for a weird problem. I have a controller, that needs authentication (with the devise gem). I added the Devise TestHelpers but i can\'t get it worki

3条回答
  •  北荒
    北荒 (楼主)
    2021-02-06 03:05

    Are you using Devise with confirmable? In this case, create is not enough and you need to confirm the user with @user.confirm!

    Second, why do you create the user in the functional test? Declare your users in the fixture like this (confirmed_at if you require confirmation only):

    test/fixtures/users.yml:

    user1: 
    id: 1
    email: user1@test.eu
    encrypted_password: abcdef1
    password_salt:  efvfvffdv
    confirmed_at: <%= Time.now %>
    

    and sign them in in your functional tests with:

    sign_in users(:user1)
    

    Edit: I just saw, that in my app the Devise-Testhelpers are declared in test/test-helpers.rb and I don't know if this makes a difference, maybe you want to try:

    ENV["RAILS_ENV"] = "test"
    require File.expand_path('../../config/environment', __FILE__)
    require 'rails/test_help'
    
    class ActionController::TestCase
      include Devise::TestHelpers
    end
    
    class ActiveSupport::TestCase
      # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
      #
      # Note: You'll currently still have to declare fixtures explicitly in integration tests
      # -- they do not yet inherit this setting
      fixtures :all
    
      # Add more helper methods to be used by all tests here...
    end
    

提交回复
热议问题