0

我正在使用工厂女孩来创建我的对象。我想知道为什么我必须执行以下操作:

RSpec.describe V1::SlotsController, type: :controller do
  let(:valid_slot) { create(:slot) }
  let(:valid_attributes) { attributes_for(:slot) }

  describe "DELETE destroy" do
    it "destroys the requested slot" do
      slot = Slot.create! valid_attributes # not working without this line
      expect {
        delete :destroy, { id: slot.id }
      }.to change(Slot, :count).by(-1)
    end
  end
end

如果我不覆盖插槽,而只使用 factory_girl 创建的插槽,则测试不会通过。为什么?

4

1 回答 1

1

因为let“懒加载”。你应该使用

let!(:slot) { create(:slot) }

describe "DELETE destroy" do
  it "destroys the requested slot" do
    expect {
      delete :destroy, { id: slot.id }
    }.to change(Slot, :count).by(-1)
  end
end
于 2014-10-15T10:22:38.850 回答