Created
December 2, 2020 21:22
-
-
Save pirj/c18ffbd7321d63103e491eb50c4b0f7c to your computer and use it in GitHub Desktop.
RSpec self-generating example groups
This file contains 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
RSpec.shared_examples 'with different zones', :within_time_zones do | |
block = metadata[:block] # the block passed to `context 'some test'` | |
parent_example_group = superclass # the `describe 'root'` one | |
# around { } # skip running examples in `context 'some test'` | |
# or | |
# metadata[:skip] = true # skip the whole `context 'some test'` | |
# or | |
parent_example_group.children.delete(self) | |
metadata[:within_time_zones].each do |time_zone| | |
# generates a group in the parent | |
group = parent_example_group.context "with tz=#{time_zone}", &block | |
group.around do |example| | |
$tz = time_zone | |
example.run | |
$tz = nil | |
end | |
group.let(:tz) { time_zone } # optional | |
end | |
end | |
RSpec.describe 'root' do | |
describe 'inner' do | |
context 'some test', within_time_zones: ['Madrid', 'Minsk'] do | |
it { expect(tz).to eq(tz) } | |
it { expect($tz).to eq($tz) } | |
end | |
context '2' do | |
it '22' do | |
expect(22).to eq 22 | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment