These are simple examples of testing in parallel with different mocking/stubbing. Each file tests a different mocking library, but they all use Minitest as the test runner so we can take advantage of the parallel runner. Each file passes without being run in parallel. All except minitest fail when run in parallel.
Last active
August 29, 2015 14:01
-
-
Save ordinaryzelig/f3a48b2456eac28bbbca to your computer and use it in GitHub Desktop.
Test cases for thread-safety in mocking libraries (http://redningja.com/dev/parallel-tests-with-single-database/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruby-2.0.0-p353 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'mocha', '1.1.0' | |
gem 'rspec-mocks', '3.0.0.beta2' | |
#gem 'rspec-mocks', '2.14.6' | |
gem 'minitest' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GEM | |
remote: https://rubygems.org/ | |
specs: | |
metaclass (0.0.4) | |
minitest (5.3.4) | |
mocha (1.1.0) | |
metaclass (~> 0.0.1) | |
rspec-mocks (3.0.0.beta2) | |
rspec-support (= 3.0.0.beta2) | |
rspec-support (3.0.0.beta2) | |
PLATFORMS | |
ruby | |
DEPENDENCIES | |
minitest | |
mocha (= 1.1.0) | |
rspec-mocks (= 3.0.0.beta2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# These pass. | |
require 'minitest/autorun' | |
require 'minitest/pride' | |
concurrency = 10 | |
Minitest.parallel_executor = Minitest::Parallel::Executor.new(concurrency) | |
Minitest::Test.parallelize_me! | |
class SomeClass | |
def some_method | |
end | |
end | |
describe 'Minitest Object#stub' do | |
concurrency.times do |idx| | |
it "tests object stub #{idx}" do | |
obj = SomeClass.new | |
obj.stub :some_method, idx do | |
sleep 1 | |
obj.some_method.must_equal idx | |
end | |
end | |
end | |
end | |
describe Minitest::Mock do | |
concurrency.times do |idx| | |
it "tests Mock #{idx}" do | |
obj = Minitest::Mock.new | |
obj.expect :some_method, idx, [idx] | |
sleep 1 | |
obj.some_method(idx).must_equal idx | |
obj.verify | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# These fail. | |
require 'minitest/autorun' | |
require 'minitest/pride' | |
require 'mocha/setup' | |
concurrency = 10 | |
Minitest.parallel_executor = Minitest::Parallel::Executor.new(concurrency) | |
Minitest::Test.parallelize_me! | |
class SomeClass | |
def some_method | |
end | |
end | |
describe 'Minitest Object#stub' do | |
concurrency.times do |idx| | |
it "tests object stub #{idx}" do | |
obj = SomeClass.new | |
obj.stubs(:some_method => idx) | |
sleep 1 | |
obj.some_method.must_equal idx | |
end | |
end | |
end | |
describe Minitest::Mock do | |
concurrency.times do |idx| | |
it "tests Mock #{idx}" do | |
obj = mock | |
obj.expects(:some_method).returns(idx) | |
sleep 1 | |
obj.some_method(idx).must_equal idx | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# These fail. | |
require 'minitest' | |
require 'minitest/spec' | |
Minitest.autorun | |
require 'minitest/pride' | |
require 'rspec/mocks' | |
concurrency = 10 | |
Minitest.parallel_executor = Minitest::Parallel::Executor.new(concurrency) | |
Minitest::Test.parallelize_me! | |
class MiniTest::Spec | |
before do |test_case| | |
RSpec::Mocks.setup(test_case) | |
end | |
after do |test_case| | |
begin | |
RSpec::Mocks.verify | |
ensure | |
RSpec::Mocks.teardown | |
end | |
end | |
end | |
class SomeClass | |
end | |
describe 'stubbing' do | |
concurrency.times do |idx| | |
it "tests stubbing #{idx}" do | |
obj = SomeClass.new | |
obj.stub(:num).and_return(idx) | |
sleep 1 | |
obj.num.must_equal idx | |
end | |
end | |
end | |
describe 'mocking' do | |
concurrency.times do |idx| | |
it "tests mocking #{idx}" do | |
obj = double | |
expect(obj).to receive(:num).and_return(idx) | |
sleep 1 | |
obj.num.must_equal idx | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# These fail. | |
require 'minitest' | |
require 'minitest/spec' | |
Minitest.autorun | |
require 'minitest/pride' | |
require 'rspec/mocks' | |
RSpec::Mocks::Syntax.disable_expect | |
RSpec::Mocks::Syntax.enable_expect Minitest::Spec | |
concurrency = 10 | |
Minitest.parallel_executor = Minitest::Parallel::Executor.new(concurrency) | |
Minitest::Test.parallelize_me! | |
class MiniTest::Spec | |
include RSpec::Mocks::ExampleMethods | |
before do |test_case| | |
RSpec::Mocks.setup | |
end | |
after do |test_case| | |
begin | |
RSpec::Mocks.verify | |
ensure | |
RSpec::Mocks.teardown | |
end | |
end | |
end | |
class SomeClass | |
end | |
describe 'stubbing' do | |
concurrency.times do |idx| | |
it "tests object stub #{idx}" do | |
obj = SomeClass.new | |
allow(obj).to receive(:num).and_return(idx) | |
sleep 1 | |
obj.num.must_equal idx | |
end | |
end | |
end | |
describe 'mocking' do | |
concurrency.times do |idx| | |
it "tests object stub #{idx}" do | |
obj = double | |
expect(obj).to receive(:num).and_return(idx) | |
sleep 1 | |
obj.num.must_equal idx | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment