有没有一种简单的方法可以将第一个参数传递给Rspec的`describe`?

时间:2012-11-21 18:07:24

标签: ruby rspec

假设我有一个测试套件,我首先给describe一个常量:

describe SomeExcessivelyLong::And::Maybe::Namespaced::Classname do

  # Convenience reference for tests
  let(:klass) { SomeExcessivelyLong::And::Maybe::Namespaced::Classname }

end

是否存在使subject多余的现有方法,例如let(:klass)或类似的方法?如何访问传递给describe的第一个值?

2 个答案:

答案 0 :(得分:5)

默认情况下,subject将是describe块中类的新实例,因此您应该可以调用subject.class来获取类本身。

describe SomeExcessivelyLong::And::Maybe::Namespaced::Classname do
  it "should do something" do
    subject.class.should be_a SomeExcessivelyLong::And::Maybe::Namespaced::Classname
  end
end

答案 1 :(得分:2)

在您的示例中调用described_class

describe SomeExcessivelyLong::And::Maybe::Namespaced::Classname do
  it "should do something" do
    described_class.should be SomeExcessivelyLong::And::Maybe::Namespaced::Classname
  end
end