1
일부 spec_helper.rb 메서드를 사용하여 RSpec 예제를 생성하고 있으며 그 중 하나는 다른 RSpec 예제의 데이터에 종속되어 있습니다.RSpec에서 어떻게 인스턴스 변수를 예제 블록에 전달할 수 있습니까?
예 spec_helper.rb
:
def it_should_be_big(objects)
@tested_objects ||= []
objects.each do |obj|
it "should be big (#{obj.name})" do
obj.should be_big
end
end
@tested_objects += objects
end
def it_should_be_small(objects)
@tested_objects ||= []
objects.each do |obj|
it "should be small (#{obj.name})" do
obj.should be_small
end
end
@tested_objects += objects
end
def it_should_have_tested_for_all_objects
it "should test for all objects" do
@tested_objects ||= []
(all_objects - @tested_objects).should == []
@tested_objects = []
end
end
예 something_spec.rb
:
describe "something" do
it_should_be_big(some_objects)
it_should_be_small(some_other_objects)
it_should_have_tested_for_all_objects
end
나는 코드가 훨씬 이해가되지 않습니다 알고 있지만 그것이 중요한 실제 코드합니다 (@tested_objects
변수)를 다음과 같습니다.
사양을 실행할 때 @tested_objects 변수를 찾을 수 없습니다 (예제 블록 내부에 다른 변수 공간이 사용 된 것 같습니다). 최종 도우미 메서드의 예제 블록 안에 변수를 전달하는 방법이 있습니까?
RSpec에 2.5, 당신은 before
또는 share_examples_for
을 할 수 있습니다 루비 1.8.7
이것은 상황에 그럴듯하지 않습니다. 여러 예제 (좋지 않음) 사이에서 변수를 공유하거나 예제 외부에서 변수를 설정해야하지만 예제 내부에서 액세스해야합니다. – Kostas
@vrinek : 가능한 해결책은 편집을 참조하십시오. 그러나, 나의 겸허 한 의견에서, 당신은 그런 헬퍼를 사용해서는 안됩니다 :) –