Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. class BaseWorker
  2.  
  3. def execute
  4. logger.info 'Starting the worker...'
  5. end
  6.  
  7. end
  8.  
  9. describe BaseWorker do
  10.  
  11. it 'should log an info message' do
  12. base_worker = BaseWorker.new
  13. logger_mock = double('Logging::Rails').as_null_object
  14. Logging::Rails.stub_chain(:logger, :info).and_return(logger_mock)
  15.  
  16. logger_mock.should_receive(:info).with('Starting the worker...')
  17. base_worker.execute
  18. Logging::Rails.unstub(:logger)
  19. end
  20.  
  21. end
  22.  
  23. Failure/Error: logger_mock.should_receive(:info).with('Starting worker...')
  24. (Double "Logging::Rails").info("Starting worker...")
  25. expected: 1 time
  26. received: 0 times
  27.  
  28. class BaseWorker
  29.  
  30. attr_accessor :log
  31.  
  32. def initialize
  33. @log = logger
  34. end
  35.  
  36. def execute
  37. @log.info 'Starting the worker...'
  38. end
  39.  
  40. end
  41.  
  42. describe BaseWorker do
  43. it 'should log an info message' do
  44. base_worker = BaseWorker.new
  45. logger_mock = double('logger')
  46. base_worker.log = logger_mock
  47.  
  48. logger_mock.should_receive(:info).with('Starting the worker...')
  49. base_worker.execute
  50. end
  51. end
  52.  
  53. Rails.logger.should_receive(:info).with("some message")
  54.  
  55. expect(Rails.logger).to receive(:info).with("some message")
  56.  
  57. Failure/Error: expect(Rails.logger).to receive(:info).with("some message")
  58. (#<ActiveSupport::Logger:0x007f27f72136c8>).info("some message")
  59. expected: 1 time with arguments: ("some message")
  60. received: 0 times
  61.  
  62. Rails.logger.error "Some useful error message"
  63.  
  64. expect(Rails.logger).to receive(:error).with(/error message/)
  65.  
  66. expect(Rails.logger).to receive(:error).with(/error message/).and_call_original
  67.  
  68. Rails.logger.error "Technical Error Message"
  69. Rails.logger.error "User-friendly Error Message"
  70.  
  71. expect(Rails.logger).to receive(:error).ordered
  72. expect(Rails.logger).to receive(:error).with(/User-friendly Error /).ordered.and_call_original
  73.  
  74. expect(Rails.logger).to receive(:debug).with("Technical Error Message").ordered.and_call_original
  75. expect(Rails.logger).to receive(:debug).at_least(:once).with(instance_of(String)).ordered
  76.  
  77. expect { my_method }.to output("my message").to_stdout
  78. expect { my_method }.to output("my error").to_stderr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement