File: sequence_context_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 (189 lines) | stat: -rw-r--r-- 5,874 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
describe "sequences are evaluated in the correct context, directly & implicitly" do
  include FactoryBot::Syntax::Methods

  before do
    define_class("User") do
      attr_accessor :id, :name

      def awesome
        "aw yeah"
      end
    end
  end

  it "builds a sequence calling sprintf correctly" do
    FactoryBot.define do
      factory :sequence_with_sprintf, class: User do
        sequence(:id) { |n| sprintf("foo%d", n) }
      end
    end

    expect(FactoryBot.build(:sequence_with_sprintf).id).to eq "foo1"
    expect(build(:sequence_with_sprintf).id).to eq "foo2"
  end

  it "invokes the correct method on the instance" do
    FactoryBot.define do
      factory :sequence_with_public_method, class: User do
        sequence(:id) { public_method(:awesome).call }
      end
    end

    expect(FactoryBot.build(:sequence_with_public_method).id).to eq "aw yeah"
    expect(build(:sequence_with_public_method).id).to eq "aw yeah"
  end

  it "invokes a method with no arguments on the instance" do
    FactoryBot.define do
      factory :sequence_with_frozen, class: User do
        sequence(:id) { frozen? }
      end
    end

    expect(FactoryBot.build(:sequence_with_frozen).id).to be false
    expect(build(:sequence_with_frozen).id).to be false
  end

  it "allows direct reference of a method in a sequence" do
    FactoryBot.define do
      factory :sequence_referencing_attribute_directly, class: User do
        sequence(:id) { |n| "#{awesome}#{n}" }
      end
    end
    expect(FactoryBot.build(:sequence_referencing_attribute_directly).id).to eq "aw yeah1"
    expect(build(:sequence_referencing_attribute_directly).id).to eq "aw yeah2"
  end

  context "with inherited factories" do
    it "uses the parent's sequenced attribute" do
      FactoryBot.define do
        factory :parent, class: User do
          sequence(:id) { |n| "id_#{n}" }
          factory :child, class: User
        end
      end

      parents = FactoryBot.build_list(:parent, 3)
      expect(parents[0].id).to eq "id_1"
      expect(parents[1].id).to eq "id_2"
      expect(parents[2].id).to eq "id_3"

      children = build_list(:child, 3)
      expect(children[0].id).to eq "id_4"
      expect(children[1].id).to eq "id_5"
      expect(children[2].id).to eq "id_6"
    end

    it "invokes the parent's sequenced trait from within a child's inherited trait" do
      FactoryBot.define do
        sequence :global_seq
        factory :parent, class: User do
          trait :with_sequenced_id do
            sequence(:id) { |n| "id_#{n}" }
          end

          factory :child, class: User
        end
      end

      parent_ids = FactoryBot.build_list(:parent, 3, :with_sequenced_id).map(&:id)
      expect(parent_ids).to eq ["id_1", "id_2", "id_3"]

      child_ids = build_list(:child, 3, :with_sequenced_id).map(&:id)
      expect(child_ids).to eq ["id_4", "id_5", "id_6"]
    end

    it "invokes the child's sequenced trait from within the child's own trait" do
      FactoryBot.define do
        sequence :global_sequence
        factory :parent, class: User do
          trait :with_sequenced_id do
            sequence(:id) { |n| "id_#{n}" }
          end

          factory :child, class: User, aliases: [:toddler, :teen] do
            sequence(:id) { |n| "id_#{n}" }
            trait :with_own_sequence do
              sequence(:id, 1000, aliases: [:woo_hoo, :woo_hoo_2]) { |n| "id_#{n}" }
            end
          end
        end
      end

      parents = FactoryBot.build_list(:parent, 3, :with_sequenced_id)
      expect(parents[0].id).to eq "id_1"
      expect(parents[1].id).to eq "id_2"
      expect(parents[2].id).to eq "id_3"

      children = build_list(:child, 3)
      expect(children[0].id).to eq "id_1"
      expect(children[1].id).to eq "id_2"
      expect(children[2].id).to eq "id_3"

      children = FactoryBot.build_list(:child, 3, :with_sequenced_id, :with_own_sequence)
      expect(children[0].id).to eq "id_1000"
      expect(children[1].id).to eq "id_1001"
      expect(children[2].id).to eq "id_1002"
    end

    it "redefines a child's sequence" do
      FactoryBot.define do
        factory :parent, class: User do
          sequence(:id) { |n| "parent_#{n}" }

          factory :child, class: User do
            sequence(:id) { |n| "child_#{n}" }
          end
        end
      end

      parents = FactoryBot.build_list(:parent, 3)
      expect(parents[0].id).to eq "parent_1"
      expect(parents[1].id).to eq "parent_2"
      expect(parents[2].id).to eq "parent_3"

      children = build_list(:child, 3)
      expect(children[0].id).to eq "child_1"
      expect(children[1].id).to eq "child_2"
      expect(children[2].id).to eq "child_3"
    end

    it "maintains context separation" do
      FactoryBot.define do
        sequence(:id) { |n| "global_#{n}" }

        factory :parent, class: User do
          sequence(:id) { |n| "parent_#{n}" }

          factory :child, class: User do
            sequence(:id) { |n| "child_#{n}" }
          end
        end

        factory :sibling, class: User do
          sequence(:id) { |n| "sibling_#{n}" }
        end
      end

      globals = FactoryBot.generate_list(:id, 3)
      expect(globals[0]).to eq "global_1"
      expect(globals[1]).to eq "global_2"
      expect(globals[2]).to eq "global_3"

      parents = build_list(:parent, 3)
      expect(parents[0].id).to eq "parent_1"
      expect(parents[1].id).to eq "parent_2"
      expect(parents[2].id).to eq "parent_3"

      children = FactoryBot.build_list(:child, 3)
      expect(children[0].id).to eq "child_1"
      expect(children[1].id).to eq "child_2"
      expect(children[2].id).to eq "child_3"

      siblings = build_list(:sibling, 3)
      expect(siblings[0].id).to eq "sibling_1"
      expect(siblings[1].id).to eq "sibling_2"
      expect(siblings[2].id).to eq "sibling_3"
    end
  end
end