user_info_spec.rb 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. require 'rails_helper'
  2. RSpec.describe UserInfo do
  3. describe '#current_user_id' do
  4. it 'is nil by default' do
  5. expect(described_class.current_user_id).to be nil
  6. end
  7. it 'takes a User ID as paramter and returns it' do
  8. test_id = 99
  9. described_class.current_user_id = test_id
  10. expect(described_class.current_user_id).to eq(test_id)
  11. end
  12. end
  13. describe '#ensure_current_user_id' do
  14. let(:return_value) { 'Hello World' }
  15. it 'uses and keeps set User IDs' do
  16. test_id = 99
  17. described_class.current_user_id = test_id
  18. described_class.ensure_current_user_id do
  19. expect(described_class.current_user_id).to eq(test_id)
  20. end
  21. expect(described_class.current_user_id).to eq(test_id)
  22. end
  23. it 'sets and resets temporary User ID 1' do
  24. described_class.current_user_id = nil
  25. described_class.ensure_current_user_id do
  26. expect(described_class.current_user_id).to eq(1)
  27. end
  28. expect(described_class.current_user_id).to be nil
  29. end
  30. it 'resets current_user_id in case of an exception' do
  31. begin
  32. described_class.ensure_current_user_id do
  33. raise 'error'
  34. end
  35. rescue # rubocop:disable Lint/HandleExceptions
  36. end
  37. expect(described_class.current_user_id).to be nil
  38. end
  39. it 'passes return value of given block' do
  40. received = described_class.ensure_current_user_id do
  41. return_value
  42. end
  43. expect(received).to eq(return_value)
  44. end
  45. end
  46. end