user_info.rb 929 B

123456789101112131415161718192021
  1. # This file registers a before and after each hook callback that
  2. # resets the stored current_user_id in the UserInfo which will otherwise
  3. # persists across multiple examples.
  4. # This can lead to issues where actions were performed by a user created
  5. # via a FactoryBot factory which will get removed after the example is
  6. # completed. The UserInfo.current_user_id will persist which leads to e.g.
  7. # DB ForeignKey violation errors.
  8. # If a `:current_user_id` metadata argument is set the initial value for
  9. # UserInfo.current_user_id will be set to the arguments given value
  10. # if it's a Proc it will get evaluated
  11. RSpec.configure do |config|
  12. config.before(:each) do |example|
  13. current_user_id = example.metadata[:current_user_id]
  14. UserInfo.current_user_id = current_user_id.is_a?(Proc) ? instance_exec(&current_user_id) : current_user_id
  15. end
  16. config.after(:each) do |_example|
  17. UserInfo.current_user_id = nil
  18. end
  19. end