2013-01-11 12:05:17 -05:00
|
|
|
describe 'sequences are evaluated in the correct context' do
|
|
|
|
before do
|
|
|
|
define_class("User") do
|
|
|
|
attr_accessor :id
|
|
|
|
|
|
|
|
def awesome
|
|
|
|
'aw yeah'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build(:sequence_with_sprintf).id).to eq 'foo1'
|
2013-01-11 12:05:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2013-12-14 22:33:15 -05:00
|
|
|
sequence(:id) { |n| public_method(:awesome).call }
|
2013-01-11 12:05:17 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build(:sequence_with_public_method).id).to eq 'aw yeah'
|
2013-01-11 12:05:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2013-12-14 22:33:15 -05:00
|
|
|
sequence(:id) { |n| 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
|
|
|
|
|
|
|
|
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
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build(:sequence_referencing_attribute_directly).id).to eq 'aw yeah1'
|
2013-01-11 12:05:17 -05:00
|
|
|
end
|
|
|
|
end
|