我有一个以这种方式构建的测试套件:
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
Run Code Online (Sandbox Code Playgroud)
通常我会把这个块放在上下文之外调用expect:
it "will eat some pet fishes" do
expect { meow }.to change(PetFish, :count).by(-1)
end
Run Code Online (Sandbox Code Playgroud)
但它使代码更难阅读,因为相关代码放在其上下文之外.
您是否会考虑将两个测试更改为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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3503 次 |
| 最近记录: |