Functional testing with Rails and Devise. What to put in my fixtures?

前端 未结 4 1965
悲哀的现实
悲哀的现实 2020-12-03 01:12

Hi I\'m wanting to do some functional testing of my Rails 3 app that uses Devise and CanCan.

In my User model I have the users age, I want to test that a user can on

相关标签:
4条回答
  • 2020-12-03 01:37

    Just one more hint. This worked great for me:

    http://brandonhilkert.com/blog/managing-login-passwords-for-capybara-with-minitest-and-rails-fixtures/

    And it seems to be DRY and nice solution.

    0 讨论(0)
  • 2020-12-03 01:38

    It's working for me.

    I didn't change anything on my user fixture, it's like this:

    one:
      id: 1
      name: MyString
      email: mystring@test.com
      group_id: 2
      encrypted_password: <%= Devise.bcrypt(User, 'password') %>
    

    But I changed my test_helper.rb and I added this to the class ActionController (this will work for all controllers tests):

    class ActionController::TestCase
        include Devise::TestHelpers
    
        setup do
            sign_in users(:one)
        end
    end
    
    0 讨论(0)
  • 2020-12-03 01:40

    Solution for Devise pre 3.2.0

    I think this might be what you are looking for:

    User.new.send(:password_digest, 'password')
    

    It works when the salt is nil.

    And therefore in your fixture you can do this:

    one:
      email: 'some@user.com'
      encrypted_password: <%= User.new.send(:password_digest, 'password') %>
    

    Solution for Devise versions 3.2.0 to 3.5.0

    At Devise 3.2.0, a method was created precisely for this purpose (@Inkling update). For these versions the encrypted_password should be defined like this:

    encrypted_password: <%= Devise.bcrypt(User, 'password') %>
    

    where User is the class of your user model.

    Note that this only applies if you're using the default encryption algorithm (bcrypt).


    Solution for Devise versions 3.5.1 and above

    As Katarzyna has pointed out: Device.bcrypt has been deprecated in version 3.5.1. From that version on, the encrypted_password should be defined like this:

    encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>
    
    0 讨论(0)
  • 2020-12-03 01:47

    I have sorted out the problem - I was logging in the user incorrectly.

    My test should hare read:

    test "should get index" do
        @user = users(:one)
        sign_in @user
        get :index
        assert_response :success
        assert_select 'span#loggedin', :count => 1
     end
    

    This also works and eliminates a line of code:

    test "should get index" do
        sign_in users(:one)
        get :index
        assert_response :success
        assert_select 'span#loggedin', :count => 1
    end
    

    My understanding of Fixtures was lacking ...

    But back to the question - what to put in fixtures:

    one:
     email: jez@example.com
     encrypted_password: $2a$10$PoBe1MvkoGJsjMVTEjKqgeBUp.xdfzWoiDjBzQhtLAj16NqIa2fOy
     password_salt: $2a$10$PoBe1MvkoGJsjMVTEjKqge
     reset_password_token: nil
     remember_token: nil
     remember_created_at: nil
     sign_in_count: 1
     current_sign_in_at: 2011-01-02 08:31:23
     last_sign_in_at: 2011-01-02 08:31:23
     current_sign_in_ip: 127.0.0.1
     last_sign_in_ip: 127.0.0.1
     confirmation_token: nil
     confirmed_at: 2011-01-02 08:31:23
     confirmation_sent_at: 2011-01-02 08:30:59
     failed_attempts: 0
     unlock_token: nil
     locked_at: nil
     authentication_token: nil
     created_at: 2011-01-02 08:30:59
     updated_at: 2011-01-02 08:31:23
     age: 36
    

    Now it works. If there is an easier that generating a user in dev and pasting the data into a fixture please do post.

    0 讨论(0)
提交回复
热议问题