Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/factory_bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ def self.register_sequence(sequence)
sequence
end

def self.register_sequence_uuid(sequence)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather not add another public method for this. It would be nice if there was a way we could reuse register_sequence for this. Let's think about it a bit longer.

return unless sequence
sequence.uuids.each do |uuid|
sequences.register(uuid, sequence)
end
sequence
end

def self.sequence_by_name(name)
sequences.find(name)
end
Expand Down
1 change: 1 addition & 0 deletions lib/factory_bot/definition_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def method_missing(name, *args, &block)
# Except that no globally available sequence will be defined.
def sequence(name, *args, &block)
sequence = Sequence.new(name, *args, &block)
Copy link
Collaborator

@composerinteralia composerinteralia Jun 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It occurs to me that the name may not be important for these inline sequences. Could we just use some unique identifier as the name and register with register_sequence?

Thinking out loud here. What if we had an InlineSequence class or something like that whose names method just returned [object_id.to_s]?

FactoryBot.register_sequence_uuid(sequence)
add_attribute(name) { increment_sequence(sequence) }
end

Expand Down
6 changes: 6 additions & 0 deletions lib/factory_bot/sequence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ def initialize(name, *args, &proc)
end
end

def uuids
names.map do |name|
[name, @proc.try(:object_id)].join
end
end

def next(scope = nil)
if @proc && scope
scope.instance_exec(value, &@proc)
Expand Down
37 changes: 37 additions & 0 deletions spec/acceptance/sequence_resetting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
describe "FactoryBot.rewind_sequences" do
include FactoryBot::Syntax::Methods

before do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put this setup right in the test rather than a before block, since the existing test doesn't use it.

define_model('User', age: :integer)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

FactoryBot.define do
factory :user do
sequence(:id)
sequence(:age)
end
end
end

it "resets all sequences back to their starting values" do
FactoryBot.define do
sequence(:email) { |n| "somebody#{n}@example.com" }
Expand All @@ -22,4 +32,31 @@
expect(email).to eq "[email protected]"
expect(name).to eq "Joe"
end

it "resets all sequences back to their starting values for factory specific sequences" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [91/80]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

user1 = FactoryBot.create(:user)
user2 = FactoryBot.create(:user)

expect(user1.id).to eq 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can get rid of some of these expects. They are duplicating some of the other sequence tests.

I am imagining something more similar to the existing test:

it "resets inline sequences back to their starting value" do
  class User
    attr_accessor :email
  end

  FactoryBot.define do
    factory :user do
      sequence(:email) { |n| "somebody#{n}@example.com" }
    end
  end

  2.times do
    build(:user)
  end

  FactoryBot.rewind_sequences

  user = build(:user)
 
  expect(user.email).to eq "[email protected]"
end

expect(user1.age).to eq 1

expect(user2.id).to eq 2
expect(user2.age).to eq 2

User.destroy_all
FactoryBot.rewind_sequences

user1 = FactoryBot.create(:user)
user2 = FactoryBot.create(:user)
user3 = FactoryBot.create(:user)

expect(user1.id).to eq 1
expect(user1.age).to eq 1

expect(user2.id).to eq 2
expect(user2.age).to eq 2

expect(user3.id).to eq 3
expect(user3.age).to eq 3
end
end