Rspec-core: 곡유 μ»¨ν…μŠ€νŠΈλŠ” ν™•μž₯ λ©”μ„œλ“œμ—μ„œ μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

에 λ§Œλ“  2019λ…„ 12μ›” 16일  Β·  5μ½”λ©˜νŠΈ  Β·  좜처: rspec/rspec-core

문제의 주제

config.extend(...) λ₯Ό 톡해 μ£Όμž… 된 λ©”μ„œλ“œλŠ” 곡유 μ»¨ν…μŠ€νŠΈ λ‚΄μ—μ„œ 호좜 될 λ•Œ μ •μ˜λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

λ‹Ήμ‹ μ˜ ν™˜κ²½

  • Ruby 버전 : 2.6
  • rspec-core 버전 : 3.9.0

μž¬ν˜„ 단계

module Foo
  def something
    puts 'hi'
  end
end

RSpec.configure do |config|
  config.extend(Foo)
end

RSpec.shared_context 'shared' do
  something
end

μ˜ˆμƒλ˜λŠ” 행동

'hi'λ₯Ό μΈμ‡„ν•©λ‹ˆλ‹€.

μ‹€μ œ 행동

something 이 μ •μ˜λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.

λͺ¨λ“  5 λŒ“κΈ€

곡유 된 μ»¨ν…μŠ€νŠΈλ₯Ό μ‚¬μš©ν•˜λŠ” 방법에 λŒ€ν•œ μ’€ 더 λ§Žμ€ μ»¨ν…μŠ€νŠΈλ₯Ό μ œκ³΅ν•΄ μ£Όμ‹œκ² μŠ΅λ‹ˆκΉŒ? 이 λ™μž‘μ„ μž¬ν˜„ ν•  수 μ—†μŠ΅λ‹ˆλ‹€.

module Foo
  def something
    puts 'hi'
  end
end

RSpec.configure do |config|
  config.extend Foo
end

RSpec.shared_context 'shared' do
  something
end

RSpec.describe do
  include_context 'shared'

  it { }
end

좜λ ₯ :

$ rspec spec/a_spec.rb
hi
Run options: exclude {:ruby=>#<Proc:./spec/spec_helper.rb:99>}

Randomized with seed 10987


  example at ./spec/a_spec.rb:18

Finished in 0.00185 seconds (files took 1.19 seconds to load)
1 example, 0 failures

Randomized with seed 10987

μ§€κΈˆμ€μ΄ 문제λ₯Ό ν•΄κ²°ν•˜κ³  λ‚΄ μͺ½μ—μ„œ 무엇이 잘λͺ»λ˜μ—ˆλŠ”지 μ‘°μ‚¬ν•˜κ² μŠ΅λ‹ˆλ‹€. 응닡 ν•΄ μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€. λ‚΄κ°€ μ•Œμ•„ λ‚΄λ©΄ λ‹€μ‹œλ³΄κ³ ν•˜κ² μŠ΅λ‹ˆλ‹€.

ꡬ성 블둝 내뢀에 μ»¨ν…μŠ€νŠΈλ₯Ό ν¬ν•¨ν•˜λ©΄ λ¬Έμ œκ°€ λ°œμƒν•œλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.

module Foo
  def something
    puts 'hi'
  end
end

RSpec.configure do |config|
  config.extend Foo
end

RSpec.shared_context 'shared' do
  something
end

RSpec.configure do |config|
  config.include_context 'shared', foo: :enabled
end

RSpec.describe "Foo", foo: :enabled do
  it { }
end

좜λ ₯ :

$ rspec spec/foo_spec.rb
An error occurred while loading ./spec/foo_spec.rb.
Failure/Error: something

NameError:
  undefined local variable or method `something' for RSpec::ExampleGroups::Foo:Class

No examples found.

Finished in 0.00017 seconds (files took 1.35 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

@tubaxenorκ°€ μ‹€νŒ¨ν•œ 예λ₯Ό μ œκ³΅ν•˜κΈ° λ•Œλ¬Έμ— @pirj λ‚˜λŠ”μ΄ 문제λ₯Ό μž¬κ°œν–ˆμŠ΅λ‹ˆλ‹€.

볡제 예λ₯Ό λ“€μ–΄ μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€.
λ‚˜λŠ” 그것이 포함 λͺ…령이라고 μƒκ°ν•©λ‹ˆλ‹€. lib/rspec/core/configuration.rb:1513 λΉ λ₯Έ 디버그 지점.

λ¨Όμ € ν™•μž₯ν•˜κ³  이후에 ν¬ν•¨ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€. λͺ¨λ“ˆμ΄ 곡유 μ»¨ν…μŠ€νŠΈμ— μ •μ˜ 된 것을 ν•„μš”λ‘œ ν•  λ•Œ 쒋은 예λ₯Ό 생각할 수 μžˆμŠ΅λ‹ˆκΉŒ?

1513 및 1514 행을 λ°”κΎΈμ–΄ μ‹œλ„ν•˜κ³  일뢀 사양 / κΈ°λŠ₯이 μ‹€νŒ¨ν•˜λŠ”μ§€ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

문제λ₯Ό μ‘°μ‹¬μŠ€λŸ½κ²Œ 닀루고 μ‹ΆμŠ΅λ‹ˆκΉŒ?

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰