Skip to content

Instantly share code, notes, and snippets.

@gingray
Created February 21, 2025 22:20
Show Gist options
  • Save gingray/16bfd334197a5693942ac8ea06465483 to your computer and use it in GitHub Desktop.
Save gingray/16bfd334197a5693942ac8ea06465483 to your computer and use it in GitHub Desktop.
rspec with allow in multithread
# frozen_string_literal: true
# ruby 3.2.2
# rspec (3.11.0)
# rspec-core (~> 3.11.0)
# rspec-expectations (~> 3.11.0)
# rspec-mocks (~> 3.11.0)
# rspec-core (3.11.0)
# rspec-support (~> 3.11.0)
# rspec-expectations (3.11.0)
# diff-lcs (>= 1.2.0, < 2.0)
# rspec-support (~> 3.11.0)
# rspec-mocks (3.11.0)
# diff-lcs (>= 1.2.0, < 2.0)
# rspec-support (~> 3.11.0)
# rspec-support (3.11.0)
class UseCase
def self.call
"x"
end
end
RSpec.describe RShade, focus: true do
context do
# In that case rspec smart and raise RSpec::Mocks::OutsideOfExampleError
# before do
# allow(UseCase).to receive(:call).and_return('z')
# end
context 'thread 1' do
it do
Thread.new do
allow(UseCase).to receive(:call).and_return('y')
end
end
end
context 'thread 2' do
it do
sleep 3
# expect to have x but will return y
expect(UseCase.call).to eq('x')
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment