2

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

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

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

end

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

4

2 回答 2

5

默认情况下subject,将是描述块中的类的新实例,因此您应该能够调用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
于 2012-11-21T18:19:25.530 回答
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
于 2012-11-21T20:41:33.917 回答