4

我正在使用的编码标准规定,一组测试函数的规范应该有一个主题,即被调用的函数。它看起来像这样:

define User do
  context :foo do
    let(:user) { FactoryGirl.create(:user) }
    subject { user.foo }
    it { ... }
  end
end

subject块的典型用途是实例化您正在测试的类:

define User do
  subject { FactoryGirl.create(:user) }
  it { ... }
end

我们的风格指南的预期效果是,我们对每个正在测试的方法都有不同的主题块。这会减慢我们的测试速度吗?如果我们subject以典型的方式使用,我们是否会从内置的记忆或其他加速中受益,因为每个班级只有一个主题块?

旁白:

我遇到过一种情况,我们的风格不起作用。使用时,any_instance.should_receive您不能遵循我们的样式指南,否则规范将始终失败。相反,您需要使用更传统的方法subject,您正在测试的对象在哪里,并在规范中调用该方法。

# passing spec
define Foo do
  before { Bar.any_instance.stub(:baz) }
  subject { FactoryGirl.create(:foo) }
  it "bazzes Bars" do
    Bar.any_instance.should_receive(:baz)
    subject.baz_the_bars
  end
end

# spec follows style guide but fails
define Foo do
  before { Bar.any_instance.stub(:baz) }
  let(:foo) { FactoryGirl.create(:foo) }
  subject { foo.baz_the_bars }

  it "bazzes Bars" do
    Bar.any_instance.should_receive(:baz)
    subject
  end
end

class Foo
  has_many :bars

  def baz_the_bars
    bars.collect do |bar|
      bar.baz
    end.count(true)
  end
end

这种风格还有其他我应该注意的问题吗?

4

1 回答 1

6

subject就像. _ context_ 他们不应该有任何额外的开销。describelet

我个人不喜欢你展示的风格,但是当我有从方法返回的非常复杂(或只是大)数据对象时,我做了类似的事情。

describe "A thing" do
  subject(:foo) { FactoryGirl.create(:foo) }

  # A very complicated object from this method
  describe "the result of calling frob" do
    subject(:result) { foo.frob }

    it { should be_active }
    it { should be_alive }
  end
end
于 2013-10-27T14:33:45.287 回答