1

如何从数组生成它测试

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 实现它?

4

1 回答 1

1

let由于 RSpec 是 DSL,因此您无法在外部上下文中使用您的测试。RSpec 在运行测试之前首先读取示例规范文件。它击中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
于 2013-05-28T18:20:21.037 回答