diff --git a/spec/posthog/send_worker_spec.rb b/spec/posthog/send_worker_spec.rb index 77d8e64..353b323 100644 --- a/spec/posthog/send_worker_spec.rb +++ b/spec/posthog/send_worker_spec.rb @@ -23,15 +23,15 @@ class PostHog end after :all do + PostHog::Defaults::Request.send(:remove_const, :BACKOFF) PostHog::Defaults::Request::BACKOFF = 30.0 end it 'does not error if the request fails' do expect do - PostHog::Transport - .any_instance - .stub(:send) - .and_return(PostHog::Response.new(-1, 'Unknown error')) + allow_any_instance_of(PostHog::Transport).to( + receive(:send).and_return(PostHog::Response.new(-1, 'Unknown error')) + ) queue = Queue.new queue << {} @@ -39,16 +39,13 @@ class PostHog worker.run expect(queue).to be_empty - - PostHog::Transport.any_instance.unstub(:send) end.to_not raise_error end it 'executes the error handler if the request is invalid' do - PostHog::Transport - .any_instance - .stub(:send) - .and_return(PostHog::Response.new(400, 'Some error')) + allow_any_instance_of(PostHog::Transport).to( + receive(:send).and_return(PostHog::Response.new(400, 'Some error')) + ) status = error = nil on_error = @@ -67,8 +64,6 @@ class PostHog sleep 0.1 # First give thread time to spin-up. sleep 0.01 while worker.is_requesting? - PostHog::Transport.any_instance.unstub(:send) - expect(queue).to be_empty expect(status).to eq(400) expect(error).to eq('Some error') @@ -118,12 +113,10 @@ def bad_obj.to_json(*_args) end it 'returns true if there is a current batch' do - PostHog::Transport - .any_instance - .stub(:send) do - sleep(0.2) - PostHog::Response.new(200, 'Success') - end + allow_any_instance_of(PostHog::Transport).to receive(:send) do + sleep(0.2) + PostHog::Response.new(200, 'Success') + end queue = Queue.new queue << Requested::CAPTURE @@ -134,8 +127,6 @@ def bad_obj.to_json(*_args) worker_thread.join expect(worker.is_requesting?).to eq(false) - - PostHog::Transport.any_instance.unstub(:send) end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index cacce92..791899b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,6 +10,12 @@ require 'active_support/all' require 'webmock/rspec' +RSpec.configure do |config| + config.before(:each) do + PostHog::Logging.logger = Logger.new('/dev/null') # Suppress all logging + end +end + # Setting timezone for ActiveSupport::TimeWithZone to UTC Time.zone = 'UTC'