g-cnbq
Last Updated: February 25, 2016
·
1.461K
· nick-desteffen
Img 2817

Lower your cost for speedy tests when using BCrypt

If you are using BCrypt Ruby you can have some significant time savings when running your unit tests by lowering the cost. By default BCrypt::Password.create(unencrypted_password) will use a cost of 10. I dropped a RSpec test suite from 25 seconds to just 4 by just setting the cost to be the minimum (4) in test mode.

def password=(unencrypted_password)
  @password = unencrypted_password
  unless unencrypted_password.blank?
    if Rails.env.test?
      cost = BCrypt::Engine::MIN_COST
    else 
      cost = BCrypt::Engine::DEFAULT_COST
    end
    self.password_digest = BCrypt::Password.create(unencrypted_password, cost: cost)
  end
end
Say Thanks
Respond

1 Response
Add your response

892
6f8526385111d6a9c4e6bc49582ae7cc

Nice tip!
You can also set it on your test_helper.

over 1 year ago ·