Skip to content

Instantly share code, notes, and snippets.

@pirj
Created December 2, 2020 21:22
Show Gist options
  • Select an option

  • Save pirj/c18ffbd7321d63103e491eb50c4b0f7c to your computer and use it in GitHub Desktop.

Select an option

Save pirj/c18ffbd7321d63103e491eb50c4b0f7c to your computer and use it in GitHub Desktop.

Revisions

  1. pirj created this gist Dec 2, 2020.
    37 changes: 37 additions & 0 deletions with_zones_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    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