changed, changed_attributes
This commit is contained in:
@@ -99,6 +99,47 @@ RSpec.describe HasAuxTable do
|
||||
expect(lot.vehicles.first.name).to eq("vehicle1")
|
||||
end
|
||||
|
||||
describe "#changed?" do
|
||||
it "returns true if the main record changes" do
|
||||
car = Car.create!(name: "Honda Civic")
|
||||
expect(car.changed?).to be_falsey
|
||||
car.name = "Toyota Camry"
|
||||
expect(car.changed?).to be_truthy
|
||||
end
|
||||
|
||||
it "returns true if the aux record changes" do
|
||||
car = Car.create!(name: "Honda Civic")
|
||||
expect(car.changed?).to be_falsey
|
||||
car.fuel_type = "hybrid"
|
||||
expect(car.changed?).to be_truthy
|
||||
end
|
||||
end
|
||||
|
||||
describe "#changed_attributes" do
|
||||
# changed_attributes returns a hash with the original values of the attribute
|
||||
|
||||
it "returns the changed attributes of the main record" do
|
||||
car = Car.create!(name: "Honda Civic")
|
||||
expect(car.changed_attributes).to eq({})
|
||||
car.name = "Toyota Camry"
|
||||
expect(car.changed_attributes).to eq({ "name" => "Honda Civic" })
|
||||
end
|
||||
|
||||
it "returns the changed attributes of the aux record when original is nil" do
|
||||
car = Car.create!(name: "Honda Civic")
|
||||
expect(car.changed_attributes).to eq({})
|
||||
car.fuel_type = "hybrid"
|
||||
expect(car.changed_attributes).to eq({ "fuel_type" => nil })
|
||||
end
|
||||
|
||||
it "returns the changed attributes of the aux record when original is not nil" do
|
||||
car = Car.create!(name: "Honda Civic", fuel_type: "gasoline")
|
||||
expect(car.changed_attributes).to eq({})
|
||||
car.fuel_type = "hybrid"
|
||||
expect(car.changed_attributes).to eq({ "fuel_type" => "gasoline" })
|
||||
end
|
||||
end
|
||||
|
||||
describe "database integration" do
|
||||
it "provides automatic attribute accessors for auxiliary table columns" do
|
||||
vehicle = Car.create!(name: "Honda Civic")
|
||||
|
||||
Reference in New Issue
Block a user