File: sequence_spec.rb

package info (click to toggle)
ruby-factory-bot 6.5.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,492 kB
  • sloc: ruby: 9,242; makefile: 6; sh: 4
file content (237 lines) | stat: -rw-r--r-- 7,027 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
shared_examples "a sequence" do |options|
  first_value = options[:first_value]
  second_value = options[:second_value]

  it "has a next value equal to its first value" do
    expect(subject.next).to eq first_value
  end

  it "has a next value equal to the 2nd value after being incremented" do
    subject.next

    expect(subject.next).to eq second_value
  end

  it "has a next value equal to the 1st value after rewinding" do
    subject.next
    subject.rewind

    expect(subject.next).to eq first_value
  end
end

describe FactoryBot::Sequence do
  describe ".find" do
    before(:each) do
      define_class("User") { attr_accessor :name }

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

    it "accepts a list of symbols" do
      expect(described_class.find(:user, :with_email, :email)).to be_truthy
    end

    it "accepts a list of strings" do
      expect(described_class.find("user", "with_email", "email")).to be_truthy
    end

    it "accepts a mixture of symbols & strings" do
      expect(described_class.find(:user, "with_email", :email)).to be_truthy
    end

    it "returns nil with a non-matching URI" do
      expect(described_class.find(:user, :email)).to be_nil
    end

    it "raises an exception with no arguments given" do
      expect { described_class.find }
        .to raise_error ArgumentError, /wrong number of arguments, expected 1\+/
    end
  end

  describe ".find_by_uri" do
    before(:each) do
      define_class("User") { attr_accessor :name }

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

    it "accepts a String" do
      expect(described_class.find_by_uri("user/with_email/email")).to be_truthy
    end

    it "accepts a Symbol" do
      expect(described_class.find_by_uri(:"user/with_email/email")).to be_truthy
    end

    it "returns nil with a non-matching URI" do
      expect(described_class.find_by_uri("user/email")).to be_nil
    end

    it "raises an exception with no arguments given" do
      expect { described_class.find_by_uri }
        .to raise_error ArgumentError, /wrong number of arguments \(given 0, expected 1\)/
    end
  end

  describe "a basic sequence" do
    let(:name) { :test }
    subject { FactoryBot::Sequence.new(name) { |n| "=#{n}" } }

    its(:name) { should eq name }
    its(:names) { should eq [name] }

    it_behaves_like "a sequence", first_value: "=1", second_value: "=2"
  end

  describe "a custom sequence" do
    subject { FactoryBot::Sequence.new(:name, "A") { |n| "=#{n}" } }

    it_behaves_like "a sequence", first_value: "=A", second_value: "=B"
  end

  describe "a sequence with aliases using default value" do
    subject do
      FactoryBot::Sequence.new(:test, aliases: [:alias, :other]) do |n|
        "=#{n}"
      end
    end

    it "has the expected names as its names" do
      names = [:foo, :bar, :baz]
      sequence = FactoryBot::Sequence.new(names.first, aliases: names.last(2)) {
        "=#{n}"
      }

      expect(sequence.names).to eq names
    end

    it_behaves_like "a sequence", first_value: "=1", second_value: "=2"
  end

  describe "a sequence with custom value and aliases" do
    subject do
      FactoryBot::Sequence.new(:test, 3, aliases: [:alias, :other]) do |n|
        "=#{n}"
      end
    end

    it "has the expected names as its names" do
      names = [:foo, :bar, :baz]
      sequence = FactoryBot::Sequence.new(names.first, 3, aliases: names.last(2)) {
        "=#{n}"
      }
      expect(sequence.names).to eq names
    end

    it_behaves_like "a sequence", first_value: "=3", second_value: "=4"
  end

  describe "a basic sequence without a block" do
    subject { FactoryBot::Sequence.new(:name) }

    it_behaves_like "a sequence", first_value: 1, second_value: 2
  end

  describe "a custom sequence without a block" do
    subject { FactoryBot::Sequence.new(:name, "A") }

    it_behaves_like "a sequence", first_value: "A", second_value: "B"
  end

  describe "a sequence with lazy initial value and a block" do
    subject do
      FactoryBot::Sequence.new(:test, proc { "J" }) do |n|
        "=#{n}"
      end
    end

    it_behaves_like "a sequence", first_value: "=J", second_value: "=K"
  end

  describe "a sequence with a lazy initial value without a block" do
    subject do
      FactoryBot::Sequence.new(:test, proc { 3 })
    end

    it_behaves_like "a sequence", first_value: 3, second_value: 4
  end

  describe "iterating over items in an enumerator" do
    subject do
      FactoryBot::Sequence.new(:name, %w[foo bar].to_enum) { |n| "=#{n}" }
    end

    it "navigates to the next items until no items remain" do
      sequence = FactoryBot::Sequence.new(:name, %w[foo bar].to_enum) { |n| "=#{n}" }
      expect(sequence.next).to eq "=foo"
      expect(sequence.next).to eq "=bar"

      expect { sequence.next }.to raise_error(StopIteration)
    end

    it_behaves_like "a sequence", first_value: "=foo", second_value: "=bar"
  end

  it "a custom sequence and scope increments within the correct scope" do
    sequence = FactoryBot::Sequence.new(:name, "A") { |n| "=#{n}#{foo}" }
    scope = double("scope", foo: "attribute")

    expect(sequence.next(scope)).to eq "=Aattribute"
  end

  it "a custom lazy sequence and scope increments within the correct scope" do
    sequence = FactoryBot::Sequence.new(:name, proc { "A" }) { |n| "=#{n}#{foo}" }
    scope = double("scope", foo: "attribute")

    expect(sequence.next(scope)).to eq "=Aattribute"
    expect(sequence.next(scope)).to eq "=Battribute"
  end

  it "a custom sequence and scope increments within the correct scope when incrementing" do
    sequence = FactoryBot::Sequence.new(:name, "A") { |n| "=#{n}#{foo}" }
    scope = double("scope", foo: "attribute")
    sequence.next(scope)

    expect(sequence.next(scope)).to eq "=Battribute"
  end

  it "a custom lazy sequence and scope increments within the correct scope when incrementing" do
    sequence = FactoryBot::Sequence.new(:name, proc { "A" }) { |n| "=#{n}#{foo}" }
    scope = double("scope", foo: "attribute")
    sequence.next(scope)

    expect(sequence.next(scope)).to eq "=Battribute"
  end

  it "a custom scope increments within the correct scope after rewinding" do
    sequence = FactoryBot::Sequence.new(:name, "A") { |n| "=#{n}#{foo}" }
    scope = double("scope", foo: "attribute")
    sequence.next(scope)
    sequence.rewind

    expect(sequence.next(scope)).to eq "=Aattribute"
  end

  it "a custom scope with a lazy sequence increments within the correct scope after rewinding" do
    sequence = FactoryBot::Sequence.new(:name, proc { "A" }) { |n| "=#{n}#{foo}" }
    scope = double("scope", foo: "attribute")
    sequence.next(scope)
    sequence.rewind

    expect(sequence.next(scope)).to eq "=Aattribute"
  end
end