ruby – 如何在阻止之前“期望”更改rspec中的某些内容?

我有一个以这种方式构建的测试套件:
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

通常我只是把这个块放在上下文调用期望之下:

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

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

解决方法

您是否会考虑更改两个测试以期望语法在相同的上下文中获取它们?也许是这样的:
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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...