10

我有一个这样构建的测试套件:

let(:cat) { create :blue_russian_cat } 
subject { cat }

context "empty bowl" do
  let!(:bowl) { create(:big_bowl, amount: 0) }
  before { meow }

  # a ton of `its` or `it` which require `meow` to be executed before making assertion
  its(:status) { should == :annoyed }
  its(:tail) { should == :straight }
  # ...

  # here I want to expect that number of PetFishes is going down after `meow`, like that
  it "will eat some pet fishes" do
    expect {???}.to change(PetFish, :count).by(-1)
  end
end

通常我会把这个块放在上下文调用之外expect

  it "will eat some pet fishes" do
    expect { meow }.to change(PetFish, :count).by(-1)
  end

但它使代码更难阅读,因为相关代码被放置在其上下文之外。

4

3 回答 3

4

您会考虑将两个测试都更改为expect语法以使它们处于相同状态context吗?也许是这样的:

let(:cat) { create :blue_russian_cat } 

context "empty bowl" do
  let!(:bowl) { create(:big_bowl, amount: 0) }
  let(:meowing) { -> { meow } } # not sure what meow is, so may not need lambda

  it "will annoy the cat" do
    expect(meowing).to change(cat.status).from(:placid).to(:annoyed)
  end

  # here I want to expect that number of PetFishes is going down after `meow`
  it "will eat some pet fishes" do
    expect(meowing).to change(PetFish, :count).by(-1)
  end
end
于 2013-05-23T01:47:48.853 回答
3

你不会对before区块设定期望。它的目的是设置环境(而且它是在规范之前执行的,所以期待它为时已晚)。你想要常规let的 .

context "empty bowl" do
  let(:cat) { meow }

  # here I want to expect that number of PetFishes is going down after `meow`, like that
  it "will eat some pet fishes" do
    expect {cat}.to change(PetFish, :count).by(-1)
  end
end
于 2013-05-22T20:28:21.590 回答
0

我意识到使用实例变量并不理想,但如果您的敏感性允许,您可以这样做:

context "empty bowl" do
  # ...
  before { @pet_fish_change = change_to(PetFish, :count) {meow} }

  it "will eat some pet fishes" do
    expect(@pet_fish_change).to eq(-1)
  end

  # ...

end

这需要您定义一个change_to辅助方法,但这非常简单:

def change_to(obj, method, &block)
  before = obj.send method
  yield
  (obj.send method) - before
end
于 2014-01-14T12:44:44.670 回答