2012-01-07 22:13:38 -05:00
|
|
|
describe "calling methods on the model instance" do
|
|
|
|
before do
|
2018-10-07 21:45:51 -04:00
|
|
|
define_model("User", age: :integer, age_copy: :integer) do
|
2012-01-07 22:13:38 -05:00
|
|
|
def age
|
|
|
|
read_attribute(:age) || 18
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2012-01-07 22:13:38 -05:00
|
|
|
factory :user do
|
|
|
|
age_copy { age }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "without the attribute being overridden" do
|
|
|
|
it "returns the correct value from the instance" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build(:user).age_copy).to eq 18
|
2012-01-07 22:13:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns nil during attributes_for" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.attributes_for(:user)[:age_copy]).to be_nil
|
2012-01-07 22:13:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't instantiate a record with attributes_for" do
|
2017-06-01 12:54:02 -04:00
|
|
|
allow(User).to receive(:new)
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.attributes_for(:user)
|
2017-06-01 12:54:02 -04:00
|
|
|
expect(User).to_not have_received(:new)
|
2012-01-07 22:13:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with the attribute being overridden" do
|
|
|
|
it "uses the overridden value" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build(:user, age_copy: nil).age_copy).to be_nil
|
2012-01-07 22:13:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the overridden value during attributes_for" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.attributes_for(:user, age_copy: 25)[:age_copy]).to eq 25
|
2012-01-07 22:13:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with the referenced attribute being overridden" do
|
|
|
|
it "uses the overridden value" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build(:user, age: nil).age_copy).to be_nil
|
2012-01-07 22:13:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the overridden value during attributes_for" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.attributes_for(:user, age: 25)[:age_copy]).to eq 25
|
2012-01-07 22:13:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|