문제

Check this out:

  it "should not be this tough" do
    was = obj.association_id
    expect(obj.association_id).to eq 1
    obj.association_id = 2 * was
    expect(obj.association_id).to eq 2
    expect(obj.association_id_changed?).to be_true
    expect(obj.association_id_was).to be_present
  end

The last expectation fails. Why is that? If I go through the same steps in the console it works as I expect.

도움이 되었습니까?

해결책

The attribute_was method will look for the value the attribute had when it was persisted for the last time. If you haven't persisted the attribute, it will return false if you run .present? or expect it to be_present.

So, in your case, I would assume you're either just writing expectations on an object that has only been instantiated but not saved, or you have saved the object with an empty or nil value for association_id.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top