-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Rewinding all sequences and not just the global ones #1078
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It occurs to me that the Thinking out loud here. What if we had an |
||
FactoryBot.register_sequence_uuid(sequence) | ||
add_attribute(name) { increment_sequence(sequence) } | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,16 @@ | |
describe "FactoryBot.rewind_sequences" do | ||
include FactoryBot::Syntax::Methods | ||
|
||
before do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's put this setup right in the test rather than a |
||
define_model('User', age: :integer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" } | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [91/80] There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can get rid of some of these 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 |
There was a problem hiding this comment.
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.