Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top