2013-05-28 56 views
1

如何產生的呢從陣列Rspec的產生「它做的」從陣列

describe "some test" do 
    let(:some) { generated_array } 

    # raise error - undefined local variable or method 
    some.each do |key| 
    it "#{key} test" do 
     true 
    end 
    end 

    # will work 
    [1,2,3].each do |key| 
    ... 

end 

測試它如何使用RSpec進行realyzed?

回答

1

由於RSpec是DSL,您不能在外部環境中使用let進行測試。在運行測試之前,RSpec首先讀取示例spec文件。在任何實際測試運行之前,它在DSL解析期間遇到some.each

此錯誤是因爲some在示例對象上得到定義,但describecontext在示例組對象上下文中運行。

describe 'thing' do 
    p self.ancestors 
    #=> [#<Class:0x007fa97a0761f8>, RSpec::Core::ExampleGroup, RSpec::Matchers, 
    # RSpec::Core::MockFrameworkAdapter, RSpec::Core::SharedExampleGroup, 
    # RSpec::Core::Pending, RSpec::Core::Extensions::InstanceEvalWithArgs, 
    # RSpec::Core::ExampleGroup::LetDefinitions, 
    # RSpec::Core::ExampleGroup::NamedSubjectPreventSuper, 
    # RSpec::Core::MemoizedHelpers, Object, PP::ObjectMixin, Kernel, 
    # BasicObject] 

    it { p selfs } 
    #=> #<RSpec::Core::ExampleGroup::Nested_1:0x007f8d1b397790 ...> 
end 

你可以看到這