File: syntax_methods_within_dynamic_attributes_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 (49 lines) | stat: -rw-r--r-- 1,398 bytes parent folder | download | duplicates (4)
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
describe "syntax methods within dynamic attributes" do
  before do
    define_model("Post", title: :string, user_id: :integer) do
      belongs_to :user

      def generate
        "generate result"
      end
    end
    define_model("User", email: :string)

    FactoryBot.define do
      sequence(:email_address) { |n| "person-#{n}@example.com" }

      factory :user do
        email { generate(:email_address) }
      end

      factory :post do
        title { generate }
        user { build(:user) }
      end
    end
  end

  it "can access syntax methods from dynamic attributes" do
    expect(FactoryBot.build(:user).email).to eq "person-1@example.com"
    expect(FactoryBot.attributes_for(:user)[:email]).to eq "person-2@example.com"
  end

  it "can access syntax methods from dynamic attributes" do
    expect(FactoryBot.build(:post).user).to be_instance_of(User)
  end

  it "can access methods already existing on the class" do
    expect(FactoryBot.build(:post).title).to eq "generate result"
    expect(FactoryBot.attributes_for(:post)[:title]).to be_nil
  end

  it "allows syntax methods to be used when the block has an arity of 1" do
    FactoryBot.define do
      factory :post_using_block_with_variable, parent: :post do
        user { |_| build(:user) }
      end
    end

    expect(FactoryBot.build(:post_using_block_with_variable).user).to be_instance_of(User)
  end
end