File: definition_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 (52 lines) | stat: -rw-r--r-- 1,223 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
describe "an instance generated by a factory with a custom class name" do
  before do
    define_model("User", admin: :boolean)

    FactoryBot.define do
      factory :user

      factory :admin, class: User do
        admin { true }
      end
    end
  end

  subject { FactoryBot.create(:admin) }

  it { should be_kind_of(User) }
  it { should be_admin }
end

describe "attributes defined using Symbol#to_proc" do
  before do
    define_model("User", password: :string, password_confirmation: :string)

    FactoryBot.define do
      factory :user do
        password { "foo" }
        password_confirmation(&:password)
      end
    end
  end

  it "assigns values correctly" do
    user = FactoryBot.build(:user)

    expect(user.password).to eq "foo"
    expect(user.password_confirmation).to eq "foo"
  end

  it "assigns value with override correctly" do
    user = FactoryBot.build(:user, password: "bar")

    expect(user.password).to eq "bar"
    expect(user.password_confirmation).to eq "bar"
  end

  it "assigns overridden value correctly" do
    user = FactoryBot.build(:user, password_confirmation: "bar")

    expect(user.password).to eq "foo"
    expect(user.password_confirmation).to eq "bar"
  end
end