File: stub_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 (168 lines) | stat: -rw-r--r-- 4,005 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
describe "a stubbed instance" do
  include FactoryBot::Syntax::Methods

  before do
    define_model("User")

    define_model("Post", user_id: :integer) do
      belongs_to :user
    end

    FactoryBot.define do
      factory :user

      factory :post do
        user
      end
    end
  end

  subject { build_stubbed(:post) }

  it "acts as if it came from the database" do
    should_not be_new_record
  end

  it "assigns associations and acts as if it is saved" do
    expect(subject.user).to be_kind_of(User)
    expect(subject.user).not_to be_new_record
  end
end

describe "a stubbed instance with timestamps" do
  include FactoryBot::Syntax::Methods

  before do
    define_model("ModelWithTimestamps", created_at: :datetime, updated_at: :datetime)

    FactoryBot.define do
      factory :model_with_timestamps
    end
  end

  subject { build_stubbed(:model_with_timestamps) }

  it "assigns the exact same datetime" do
    expect(subject.created_at).to eq(subject.updated_at)
  end
end

describe "a stubbed instance overriding strategy" do
  include FactoryBot::Syntax::Methods

  before do
    define_model("User")
    define_model("Post", user_id: :integer) do
      belongs_to :user
    end

    FactoryBot.define do
      factory :user

      factory :post do
        association(:user, strategy: :build)
      end
    end
  end

  subject { build_stubbed(:post) }

  it "acts as if it is saved in the database" do
    should_not be_new_record
  end

  it "assigns associations and acts as if it is saved" do
    expect(subject.user).to be_kind_of(User)
    expect(subject.user).not_to be_new_record
  end
end

describe "overridden primary keys conventions" do
  describe "a stubbed instance with a uuid primary key" do
    it "builds a stubbed instance" do
      using_model("ModelWithUuid", primary_key: :uuid) do
        FactoryBot.define do
          factory :model_with_uuid
        end

        model = FactoryBot.build_stubbed(:model_with_uuid)
        expect(model).to be_truthy
      end
    end

    it "behaves like a persisted record" do
      using_model("ModelWithUuid", primary_key: :uuid) do
        FactoryBot.define do
          factory :model_with_uuid
        end

        model = FactoryBot.build_stubbed(:model_with_uuid)
        expect(model).to be_persisted
        expect(model).not_to be_new_record
      end
    end

    it "has a uuid primary key" do
      using_model("ModelWithUuid", primary_key: :uuid) do
        FactoryBot.define do
          factory :model_with_uuid
        end

        model = FactoryBot.build_stubbed(:model_with_uuid)
        expect(model.id).to be_a(String)
      end
    end
  end

  describe "a stubbed instance with no primary key" do
    it "builds a stubbed instance" do
      using_model("ModelWithoutPk", primary_key: false) do
        FactoryBot.define do
          factory :model_without_pk
        end

        model = FactoryBot.build_stubbed(:model_without_pk)
        expect(model).to be_truthy
      end
    end

    it "behaves like a persisted record" do
      using_model("ModelWithoutPk", primary_key: false) do
        FactoryBot.define do
          factory :model_without_pk
        end

        model = FactoryBot.build_stubbed(:model_without_pk)
        expect(model).to be_persisted
        expect(model).not_to be_new_record
      end
    end
  end

  describe "a stubbed instance with no id setter" do
    it "builds a stubbed instance" do
      FactoryBot.define do
        factory :model_hash, class: Hash
      end

      model = FactoryBot.build_stubbed(:model_hash)
      expect(model).to be_truthy
    end
  end

  def using_model(name, primary_key:)
    define_class(name, ActiveRecord::Base)

    connection = ActiveRecord::Base.connection
    begin
      clear_generated_table(name.tableize)
      connection.create_table(name.tableize, id: primary_key) do |t|
        t.column :updated_at, :datetime
      end

      yield
    ensure
      clear_generated_table(name.tableize)
    end
  end
end