File: overrides_spec.rb

package info (click to toggle)
ruby-factory-bot 6.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 1,372 kB
  • sloc: ruby: 7,827; makefile: 6
file content (60 lines) | stat: -rw-r--r-- 1,288 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
describe "attribute overrides" do
  before do
    define_model("User", admin: :boolean)
    define_model("Post", title: :string,
      secure: :boolean,
      user_id: :integer) do
      belongs_to :user

      def secure=(value)
        return unless user&.admin?

        write_attribute(:secure, value)
      end
    end

    FactoryBot.define do
      factory :user do
        factory :admin do
          admin { true }
        end
      end

      factory :post do
        user
        title { "default title" }
      end
    end
  end

  let(:admin) { FactoryBot.create(:admin) }

  let(:post_attributes) do
    {secure: false}
  end

  let(:non_admin_post_attributes) do
    post_attributes[:user] = FactoryBot.create(:user)
    post_attributes
  end

  let(:admin_post_attributes) do
    post_attributes[:user] = admin
    post_attributes
  end

  context "with an admin posting" do
    subject { FactoryBot.create(:post, admin_post_attributes) }
    its(:secure) { should eq false }
  end

  context "with a non-admin posting" do
    subject { FactoryBot.create(:post, non_admin_post_attributes) }
    its(:secure) { should be_nil }
  end

  context "with no user posting" do
    subject { FactoryBot.create(:post, post_attributes) }
    its(:secure) { should be_nil }
  end
end