你想extend
,不include
。这适用于红宝石1.9.3,例如:
module Foo
X = 123
end
describe "specs with modules extended" do
extend Foo
p X # => 123
end
另外,如果你想重用在不同的测试一个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
如果你想重用在不同的规格上下文,使用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
您正在使用哪个Ruby版本? – 2012-02-04 14:16:58
@John,MRI 1.9.1 – 2012-02-04 14:31:30