File: global_to_create_spec.rb

package info (click to toggle)
ruby-factory-bot 6.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,372 kB
  • sloc: ruby: 7,827; makefile: 6
file content (126 lines) | stat: -rw-r--r-- 3,240 bytes parent folder | download | duplicates (3)
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
describe "global to_create" do
  before do
    define_model("User", name: :string)
    define_model("Post", name: :string)

    FactoryBot.define do
      to_create { |instance| instance.name = "persisted!" }

      trait :override_to_create do
        to_create { |instance| instance.name = "override" }
      end

      factory :user do
        name { "John Doe" }

        factory :child_user

        factory :child_user_with_trait do
          override_to_create
        end
      end

      factory :post do
        name { "Great title" }

        factory :child_post

        factory :child_post_with_trait do
          override_to_create
        end
      end
    end
  end

  it "handles base to_create" do
    expect(FactoryBot.create(:user).name).to eq "persisted!"
    expect(FactoryBot.create(:post).name).to eq "persisted!"
  end

  it "handles child to_create" do
    expect(FactoryBot.create(:child_user).name).to eq "persisted!"
    expect(FactoryBot.create(:child_post).name).to eq "persisted!"
  end

  it "handles child to_create with trait" do
    expect(FactoryBot.create(:child_user_with_trait).name).to eq "override"
    expect(FactoryBot.create(:child_post_with_trait).name).to eq "override"
  end

  it "handles inline trait override" do
    user = FactoryBot.create(:child_user, :override_to_create)
    post = FactoryBot.create(:child_post, :override_to_create)

    expect(user.name).to eq "override"
    expect(post.name).to eq "override"
  end

  it "uses to_create globally across FactoryBot.define" do
    define_model("Company", name: :string)

    FactoryBot.define do
      factory :company
    end

    company = FactoryBot.create(:company)
    override_company = FactoryBot.create(:company, :override_to_create)

    expect(company.name).to eq "persisted!"
    expect(override_company.name).to eq "override"
  end
end

describe "global skip_create" do
  before do
    define_model("User", name: :string)
    define_model("Post", name: :string)

    FactoryBot.define do
      skip_create

      trait :override_to_create do
        to_create { |instance| instance.name = "override" }
      end

      factory :user do
        name { "John Doe" }

        factory :child_user

        factory :child_user_with_trait do
          override_to_create
        end
      end

      factory :post do
        name { "Great title" }

        factory :child_post

        factory :child_post_with_trait do
          override_to_create
        end
      end
    end
  end

  it "does not persist any record" do
    expect(FactoryBot.create(:user)).to be_new_record
    expect(FactoryBot.create(:post)).to be_new_record
  end

  it "does not persist child records" do
    expect(FactoryBot.create(:child_user)).to be_new_record
    expect(FactoryBot.create(:child_post)).to be_new_record
  end

  it "honors overridden to_create" do
    expect(FactoryBot.create(:child_user_with_trait).name).to eq "override"
    expect(FactoryBot.create(:child_post_with_trait).name).to eq "override"
  end

  it "honors inline trait to_create" do
    expect(FactoryBot.create(:child_user, :override_to_create).name).to eq "override"
    expect(FactoryBot.create(:child_post, :override_to_create).name).to eq "override"
  end
end