如何将模块混合到rspec上下文中

时间:2021-12-13 15:59:01

How can I mix a module into an rspec context (aka describe), such that the module's constants are available to the spec?

如何将模块混合到rspec上下文(也就是描述)中,以便模块的常量可用于规范?

module Foo
  FOO = 1
end

describe 'constants in rspec' do

  include Foo

  p const_get(:FOO)    # => 1
  p FOO                # uninitialized constant FOO (NameError)

end

That const_get can retrieve the constant when the name of the constant cannot is interesting. What's causing rspec's curious behavior?

当常量的名称不能引起兴趣时,const_get可以检索常量。是什么导致了rspec的好奇行为?

I am using MRI 1.9.1 and rspec 2.8.0. The symptoms are the same with MRI 1.8.7.

我正在使用MRI 1.9.1和rspec 2.8.0。 MRI 1.8.7的症状相同。

2 个解决方案

#1


10  

You can use RSpec's shared_context:

您可以使用RSpec的shared_context:

shared_context 'constants' do
  FOO = 1
end

describe Model do
  include_context 'constants'

  p FOO    # => 1
end

#2


11  

You want extend, not include. This works in Ruby 1.9.3, for instance:

你想要扩展,而不是包括。这适用于Ruby 1.9.3,例如:

module Foo
  X = 123
end

describe "specs with modules extended" do
  extend Foo
  p X # => 123
end

Alternatively, if you want to reuse an RSpec context across different tests, use shared_context:

或者,如果要在不同的测试中重用RSpec上下文,请使用shared_context:

shared_context "with an apple" do
  let(:apple) { Apple.new }
end

describe FruitBasket do
  include_context "with an apple"

  it "should be able to hold apples" do
    expect { subject.add apple }.to change(subject, :size).by(1)
  end
end

If you want to reuse specs across different contexts, use shared_examples and it_behaves_like:

如果要在不同的上下文中重用规范,请使用shared_examples和it_behaves_like:

shared_examples "a collection" do
  let(:collection) { described_class.new([7, 2, 4]) }

  context "initialized with 3 items" do
    it "says it has three items" do
      collection.size.should eq(3)
    end
  end
end

describe Array do
  it_behaves_like "a collection"
end

describe Set do
  it_behaves_like "a collection"
end

#1


10  

You can use RSpec's shared_context:

您可以使用RSpec的shared_context:

shared_context 'constants' do
  FOO = 1
end

describe Model do
  include_context 'constants'

  p FOO    # => 1
end

#2


11  

You want extend, not include. This works in Ruby 1.9.3, for instance:

你想要扩展,而不是包括。这适用于Ruby 1.9.3,例如:

module Foo
  X = 123
end

describe "specs with modules extended" do
  extend Foo
  p X # => 123
end

Alternatively, if you want to reuse an RSpec context across different tests, use shared_context:

或者,如果要在不同的测试中重用RSpec上下文,请使用shared_context:

shared_context "with an apple" do
  let(:apple) { Apple.new }
end

describe FruitBasket do
  include_context "with an apple"

  it "should be able to hold apples" do
    expect { subject.add apple }.to change(subject, :size).by(1)
  end
end

If you want to reuse specs across different contexts, use shared_examples and it_behaves_like:

如果要在不同的上下文中重用规范,请使用shared_examples和it_behaves_like:

shared_examples "a collection" do
  let(:collection) { described_class.new([7, 2, 4]) }

  context "initialized with 3 items" do
    it "says it has three items" do
      collection.size.should eq(3)
    end
  end
end

describe Array do
  it_behaves_like "a collection"
end

describe Set do
  it_behaves_like "a collection"
end