2011-08-20 18:05:41 -04:00
|
|
|
describe "transient attributes" do
|
|
|
|
before do
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model("User", name: :string, email: :string)
|
2011-08-20 18:05:41 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2013-12-14 22:33:15 -05:00
|
|
|
sequence(:name) { |n| "John #{n}" }
|
2011-08-20 18:05:41 -04:00
|
|
|
|
|
|
|
factory :user do
|
2014-04-28 17:39:44 -04:00
|
|
|
transient do
|
2020-06-05 15:15:18 -04:00
|
|
|
four { 2 + 2 }
|
2018-07-29 11:30:02 -04:00
|
|
|
rockstar { true }
|
2020-06-05 15:15:18 -04:00
|
|
|
upcased { false }
|
2011-10-07 18:19:27 -04:00
|
|
|
end
|
2011-08-20 18:05:41 -04:00
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
name { "#{FactoryBot.generate(:name)}#{" - Rockstar" if rockstar}" }
|
2011-08-20 18:05:41 -04:00
|
|
|
email { "#{name.downcase}#{four}@example.com" }
|
|
|
|
|
2012-05-04 16:48:46 -04:00
|
|
|
after(:create) do |user, evaluator|
|
2012-02-08 10:17:57 -05:00
|
|
|
user.name.upcase! if evaluator.upcased
|
2011-08-20 18:05:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "returning attributes for a factory" do
|
2017-10-20 15:20:28 -04:00
|
|
|
subject { FactoryBot.attributes_for(:user, rockstar: true) }
|
2011-08-20 18:05:41 -04:00
|
|
|
it { should_not have_key(:four) }
|
|
|
|
it { should_not have_key(:rockstar) }
|
|
|
|
it { should_not have_key(:upcased) }
|
2020-06-05 15:15:18 -04:00
|
|
|
it { should have_key(:name) }
|
|
|
|
it { should have_key(:email) }
|
2011-08-20 18:05:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "with a transient variable assigned" do
|
2020-06-05 15:15:18 -04:00
|
|
|
let(:rockstar) { FactoryBot.create(:user, rockstar: true, four: "1234") }
|
2017-10-20 15:20:28 -04:00
|
|
|
let(:rockstar_with_name) { FactoryBot.create(:user, name: "Jane Doe", rockstar: true) }
|
2020-06-05 15:15:18 -04:00
|
|
|
let(:upcased_rockstar) { FactoryBot.create(:user, rockstar: true, upcased: true) }
|
|
|
|
let(:groupie) { FactoryBot.create(:user, rockstar: false) }
|
2011-08-20 18:05:41 -04:00
|
|
|
|
|
|
|
it "generates the correct attributes on a rockstar" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(rockstar.name).to eq "John 1 - Rockstar"
|
|
|
|
expect(rockstar.email).to eq "john 1 - rockstar1234@example.com"
|
2011-08-20 18:05:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "generates the correct attributes on an upcased rockstar" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(upcased_rockstar.name).to eq "JOHN 1 - ROCKSTAR"
|
|
|
|
expect(upcased_rockstar.email).to eq "john 1 - rockstar4@example.com"
|
2011-08-20 18:05:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "generates the correct attributes on a groupie" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(groupie.name).to eq "John 1"
|
|
|
|
expect(groupie.email).to eq "john 14@example.com"
|
2011-08-20 18:05:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "generates the correct attributes on a rockstar with a name" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(rockstar_with_name.name).to eq "Jane Doe"
|
|
|
|
expect(rockstar_with_name.email).to eq "jane doe4@example.com"
|
2011-08-20 18:05:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "without transient variables assigned" do
|
2017-10-20 15:20:28 -04:00
|
|
|
let(:rockstar) { FactoryBot.create(:user) }
|
2011-08-20 18:05:41 -04:00
|
|
|
|
|
|
|
it "uses the default value of the attribute" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(rockstar.name).to eq "John 1 - Rockstar"
|
2011-08-20 18:05:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-10-07 18:19:27 -04:00
|
|
|
|
|
|
|
describe "transient sequences" do
|
|
|
|
before do
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model("User", name: :string)
|
2011-10-07 18:19:27 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2011-10-07 18:19:27 -04:00
|
|
|
factory :user do
|
2014-04-28 17:39:44 -04:00
|
|
|
transient do
|
2011-10-07 18:19:27 -04:00
|
|
|
sequence(:counter)
|
|
|
|
end
|
|
|
|
|
|
|
|
name { "John Doe #{counter}" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "increments sequences correctly" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build(:user).name).to eq "John Doe 1"
|
|
|
|
expect(FactoryBot.build(:user).name).to eq "John Doe 2"
|
2011-10-07 18:19:27 -04:00
|
|
|
end
|
|
|
|
end
|
2012-03-13 20:12:28 -04:00
|
|
|
|
|
|
|
describe "assigning values from a transient attribute" do
|
|
|
|
before do
|
|
|
|
define_class("User") do
|
|
|
|
attr_accessor :foo_id, :foo_name
|
|
|
|
end
|
|
|
|
|
|
|
|
define_class("Foo") do
|
|
|
|
attr_accessor :id, :name
|
|
|
|
def initialize(id, name)
|
|
|
|
@id = id
|
|
|
|
@name = name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2012-03-13 20:12:28 -04:00
|
|
|
factory :user do
|
2014-04-28 17:39:44 -04:00
|
|
|
transient do
|
2018-10-07 21:45:51 -04:00
|
|
|
foo { Foo.new("id-of-foo", "name-of-foo") }
|
2012-03-13 20:12:28 -04:00
|
|
|
end
|
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
foo_id { foo.id }
|
2012-03-13 20:12:28 -04:00
|
|
|
foo_name { foo.name }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not ignore an _id attribute that is an alias for a transient attribute" do
|
2018-10-07 21:45:51 -04:00
|
|
|
user = FactoryBot.build(:user, foo: Foo.new("passed-in-id-of-foo", "passed-in-name-of-foo"))
|
|
|
|
expect(user.foo_id).to eq "passed-in-id-of-foo"
|
|
|
|
expect(user.foo_name).to eq "passed-in-name-of-foo"
|
2012-03-13 20:12:28 -04:00
|
|
|
end
|
|
|
|
end
|