File: transient_attributes_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 (122 lines) | stat: -rw-r--r-- 3,511 bytes parent folder | download | duplicates (2)
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
describe "transient attributes" do
  before do
    define_model("User", name: :string, email: :string)

    FactoryBot.define do
      sequence(:name) { |n| "John #{n}" }

      factory :user do
        transient do
          four { 2 + 2 }
          rockstar { true }
          upcased { false }
        end

        name { "#{FactoryBot.generate(:name)}#{" - Rockstar" if rockstar}" }
        email { "#{name.downcase}#{four}@example.com" }

        after(:create) do |user, evaluator|
          user.name.upcase! if evaluator.upcased
        end
      end
    end
  end

  context "returning attributes for a factory" do
    subject { FactoryBot.attributes_for(:user, rockstar: true) }
    it { should_not have_key(:four) }
    it { should_not have_key(:rockstar) }
    it { should_not have_key(:upcased) }
    it { should have_key(:name) }
    it { should have_key(:email) }
  end

  context "with a transient variable assigned" do
    let(:rockstar) { FactoryBot.create(:user, rockstar: true, four: "1234") }
    let(:rockstar_with_name) { FactoryBot.create(:user, name: "Jane Doe", rockstar: true) }
    let(:upcased_rockstar) { FactoryBot.create(:user, rockstar: true, upcased: true) }
    let(:groupie) { FactoryBot.create(:user, rockstar: false) }

    it "generates the correct attributes on a rockstar" do
      expect(rockstar.name).to eq "John 1 - Rockstar"
      expect(rockstar.email).to eq "john 1 - rockstar1234@example.com"
    end

    it "generates the correct attributes on an upcased rockstar" do
      expect(upcased_rockstar.name).to eq "JOHN 1 - ROCKSTAR"
      expect(upcased_rockstar.email).to eq "john 1 - rockstar4@example.com"
    end

    it "generates the correct attributes on a groupie" do
      expect(groupie.name).to eq "John 1"
      expect(groupie.email).to eq "john 14@example.com"
    end

    it "generates the correct attributes on a rockstar with a name" do
      expect(rockstar_with_name.name).to eq "Jane Doe"
      expect(rockstar_with_name.email).to eq "jane doe4@example.com"
    end
  end

  context "without transient variables assigned" do
    let(:rockstar) { FactoryBot.create(:user) }

    it "uses the default value of the attribute" do
      expect(rockstar.name).to eq "John 1 - Rockstar"
    end
  end
end

describe "transient sequences" do
  before do
    define_model("User", name: :string)

    FactoryBot.define do
      factory :user do
        transient do
          sequence(:counter)
        end

        name { "John Doe #{counter}" }
      end
    end
  end

  it "increments sequences correctly" do
    expect(FactoryBot.build(:user).name).to eq "John Doe 1"
    expect(FactoryBot.build(:user).name).to eq "John Doe 2"
  end
end

describe "assigning values from a transient attribute" do
  before do
    define_class("User") do
      attr_accessor :foo_id, :foo_name
    end

    define_class("Foo") do
      attr_accessor :id, :name
      def initialize(id, name)
        @id = id
        @name = name
      end
    end

    FactoryBot.define do
      factory :user do
        transient do
          foo { Foo.new("id-of-foo", "name-of-foo") }
        end

        foo_id { foo.id }
        foo_name { foo.name }
      end
    end
  end

  it "does not ignore an _id attribute that is an alias for a transient attribute" do
    user = FactoryBot.build(:user, foo: Foo.new("passed-in-id-of-foo", "passed-in-name-of-foo"))
    expect(user.foo_id).to eq "passed-in-id-of-foo"
    expect(user.foo_name).to eq "passed-in-name-of-foo"
  end
end