Skip to content

Instantly share code, notes, and snippets.

@qpSHiNqp
Created March 12, 2019 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qpSHiNqp/5f877886120ae722f4273d1c40d7bbf0 to your computer and use it in GitHub Desktop.
Save qpSHiNqp/5f877886120ae722f4273d1c40d7bbf0 to your computer and use it in GitHub Desktop.
Modules included by a context block doesn't take effect in another context block
require 'spec_helper'
describe RSpec, 'module isolation' do
module Example1
def hoge
"example1"
end
end
module Example2
def hoge
"example2"
end
end
context '#include Example1' do
include Example1
it 'should respond to :hoge and return "example1"' do
expect(self).to respond_to(:hoge)
expect(hoge).to eq("example1")
end
end
context '#include Example2' do
include Example2
it 'should respond to :hoge and return "example2"' do
expect(self).to respond_to(:hoge)
expect(hoge).to eq("example2")
end
end
context '#include nothing' do
it 'should not respond to :hoge' do
expect(self).not_to respond_to(:hoge)
end
end
end
describe RSpec do
before { ExampleJob.perform_later }
context '#include ActiveJob::TestHelper' do
include ActiveJob::TestHelper
it 'should be available to call #have_been_enqueued' do
expect { expect(ExampleJob).to have_been_enqueued }.not_to raise_error
end
it 'should be available to call #assert_enqueued_jobs' do
expect(self).to respond_to(:assert_enqueued_jobs)
assert_enqueued_jobs 1
end
it 'should change queue_adapter to :test' do
expect(ActiveJob::Base.queue_adapter).to be_kind_of(ActiveJob::QueueAdapters::TestAdapter)
end
end
context 'not #include ActiveJob::TestHelper' do
it 'should not be available to call #have_been_enqueued' do
expect { expect(ExampleJob).to have_been_enqueued }.to raise_error(StandardError)
end
it 'should not be available to call #assert_enqueued_jobs' do
expect(self).not_to respond_to(:assert_enqueued_jobs)
expect{assert_enqueued_jobs(1)}.to raise_error(StandardError)
end
it 'should not change queue_adapter to :test' do
expect(ActiveJob::Base.queue_adapter).not_to be_kind_of(ActiveJob::QueueAdapters::TestAdapter)
end
end
end
@qpSHiNqp
Copy link
Author

Background: I wanted to use ActiveJob::TestHelper only in some specific feature / context / describe block and I conducted a behavior test in such case that modules included in a specific context block and made sure that the modules doesn't take effect in the other context block in parallel.
Along with that, I determined that it is not needed to explicitly switch queue_adapter to :test if I include ActiveJob::TestHelper, as the module implicitly arranges queue_adapter for testing.

特定の spec でだけ ActiveJob::TestHelper を include して使いたかったので、特定の spec で include したモジュールが別の spec に影響を与えないことを確認する挙動テストを書いた。
また、 ActiveJob::TestHelper を include してしまえば明示的に queue_adapter:test に切り替えなくても良いことを確認した。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment