File: sequence_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 (72 lines) | stat: -rw-r--r-- 1,972 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
describe "sequences" do
  include FactoryBot::Syntax::Methods

  it "generates several values in the correct format" do
    FactoryBot.define do
      sequence :email do |n|
        "somebody#{n}@example.com"
      end
    end

    first_value = generate(:email)
    another_value = generate(:email)

    expect(first_value).to match(/^somebody\d+@example\.com$/)
    expect(another_value).to match(/^somebody\d+@example\.com$/)
    expect(first_value).not_to eq another_value
  end

  it "generates sequential numbers if no block is given" do
    FactoryBot.define do
      sequence :order
    end

    first_value = generate(:order)
    another_value = generate(:order)

    expect(first_value).to eq 1
    expect(another_value).to eq 2
    expect(first_value).not_to eq another_value
  end

  it "generates aliases for the sequence that reference the same block" do
    FactoryBot.define do
      sequence(:size, aliases: [:count, :length]) { |n| "called-#{n}" }
    end

    first_value = generate(:size)
    second_value = generate(:count)
    third_value = generate(:length)

    expect(first_value).to eq "called-1"
    expect(second_value).to eq "called-2"
    expect(third_value).to eq "called-3"
  end

  it "generates aliases for the sequence that reference the same block and retains value" do
    FactoryBot.define do
      sequence(:size, "a", aliases: [:count, :length]) { |n| "called-#{n}" }
    end

    first_value = generate(:size)
    second_value = generate(:count)
    third_value = generate(:length)

    expect(first_value).to eq "called-a"
    expect(second_value).to eq "called-b"
    expect(third_value).to eq "called-c"
  end

  it "generates few values of the sequence" do
    FactoryBot.define do
      sequence :email do |n|
        "somebody#{n}@example.com"
      end
    end

    values = generate_list(:email, 2)

    expect(values.first).to eq("somebody1@example.com")
    expect(values.second).to eq("somebody2@example.com")
  end
end