如何更改测试中的Factory girl布尔属性

时间:2016-02-06 19:03:22

标签: ruby-on-rails factory-bot rspec-rails

这是我的工厂:

factory :acceptance do
    favor
    user
    accepted false
  end

这是我的要求规范:

describe "to acceptances" do
    let (:favor) { create(:favor, user: user) }
    let (:acceptance) { create(:acceptance, user: user, favor: favor)}

    context "when has accepted acceptance" do
      it "shold not allow sending more acceptances" do
        acceptance.accepted = true
        expect(permission.allow?(:acceptances, :create, favor)).to be false
      end

问题在于acceptance.accepted = true。正如我所知,这不是将accepted属性设置为true。我怎么能做到这一点?

非常感谢你的时间!

1 个答案:

答案 0 :(得分:1)

您需要保存记录,否则您只更改了本地接受副本。添加acceptance.saveupdate_attribute将为您执行保存。

describe "to acceptances" do
  let (:favor) { create(:favor, user: user) }
  let (:acceptance) { create(:acceptance, user: user, favor: favor)}

  context "when has accepted acceptance" do
    it "shold not allow sending more acceptances" do
      acceptance.update_attribute(:accepted, true)
      expect(permission.allow?(:acceptances, :create, favor)).to be false
    end
  end
end