我有一个这样构建的测试套件:
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
但它使代码更难阅读,因为相关代码被放置在其上下文之外。