0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RSpec の describe で定数を定義するとトップレベルに定義される

Last updated at Posted at 2018-08-08
describe 'parent' do
  describe 'child A' do
    SOME_CONST = 1
    it { expect(SOME_CONST).to eq 1 }
  end
  describe 'child B' do
    SOME_CONST = 2
    it { expect(SOME_CONST).to eq 2 } # => fail
    it { expect(SOME_CONST).to eq 1 }
    it { expect(::SOME_CONST).to eq SOME_CONST }
  end
end

describe の中身は単なるブロックなので module とか class みたいに名前空間がなく、トップレベル定数になる。当たり前なんだけど、DSLだとRubyを書いてる意識が希薄でうっかりやってしまうかもしれない。

定数 - Ruby 2.5.0 リファレンスマニュアル

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?