2018-10-07 21:45:51 -04:00
|
|
|
describe "sequences are evaluated in the correct context" do
|
2013-01-11 12:05:17 -05:00
|
|
|
before do
|
|
|
|
define_class("User") do
|
|
|
|
attr_accessor :id
|
|
|
|
|
|
|
|
def awesome
|
2018-10-07 21:45:51 -04:00
|
|
|
"aw yeah"
|
2013-01-11 12:05:17 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
it "builds a sequence calling sprintf correctly" do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2013-01-11 12:05:17 -05:00
|
|
|
factory :sequence_with_sprintf, class: User do
|
2013-12-14 22:33:15 -05:00
|
|
|
sequence(:id) { |n| sprintf("foo%d", n) }
|
2013-01-11 12:05:17 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
expect(FactoryBot.build(:sequence_with_sprintf).id).to eq "foo1"
|
2013-01-11 12:05:17 -05:00
|
|
|
end
|
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
it "invokes the correct method on the instance" do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2013-01-11 12:05:17 -05:00
|
|
|
factory :sequence_with_public_method, class: User do
|
2018-10-05 14:54:08 -04:00
|
|
|
sequence(:id) { public_method(:awesome).call }
|
2013-01-11 12:05:17 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
expect(FactoryBot.build(:sequence_with_public_method).id).to eq "aw yeah"
|
2013-01-11 12:05:17 -05:00
|
|
|
end
|
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
it "invokes a method with no arguments on the instance" do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2013-01-11 12:05:17 -05:00
|
|
|
factory :sequence_with_frozen, class: User do
|
2018-10-05 14:54:08 -04:00
|
|
|
sequence(:id) { frozen? }
|
2013-01-11 12:05:17 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build(:sequence_with_frozen).id).to be false
|
2013-01-11 12:05:17 -05:00
|
|
|
end
|
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
it "allows direct reference of a method in a sequence" do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2013-01-11 12:05:17 -05:00
|
|
|
factory :sequence_referencing_attribute_directly, class: User do
|
2013-12-14 22:33:15 -05:00
|
|
|
sequence(:id) { |n| "#{awesome}#{n}" }
|
2013-01-11 12:05:17 -05:00
|
|
|
end
|
|
|
|
end
|
2018-10-07 21:45:51 -04:00
|
|
|
expect(FactoryBot.build(:sequence_referencing_attribute_directly).id).to eq "aw yeah1"
|
2013-01-11 12:05:17 -05:00
|
|
|
end
|
|
|
|
end
|