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

前端 未结 4 1972
悲哀的现实
悲哀的现实 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: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') %>
    

提交回复
热议问题