File: generators.feature

package info (click to toggle)
ruby-factory-bot-rails 6.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 316 kB
  • sloc: ruby: 635; makefile: 6; sh: 4
file content (92 lines) | stat: -rw-r--r-- 3,597 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
Feature:
  In order to easily generate factory files instead of fixture files when generating models
  As a user of Rails and Factory Bot
  I would like to use factory_bot_rails generators.

  Background:
    Given I create a new rails application
    And I add "factory_bot_rails" from this project as a dependency
    And I run `bundle install` with a clean environment

  Scenario: The factory_bot_rails generators create a factory file for each model if there is not a factories.rb file
    When I run `bundle exec rails generate model User name:string age:integer` with a clean environment
    And I run `bundle exec rails generate model Namespaced::User name:string` with a clean environment
    Then the output should contain "test/factories/users.rb"
    And the output should contain "test/factories/namespaced/users.rb"
    And the file "test/factories/users.rb" should contain exactly:
      """
      FactoryBot.define do
        factory :user do
          name { "MyString" }
          age { 1 }
        end
      end
      """
    And the file "test/factories/namespaced/users.rb" should contain "factory :namespaced_user, class: 'Namespaced::User' do"

  Scenario: The factory_bot_rails generators create a factory file with correct naming when I use --force-plural
    When I run `bundle exec rails generate model UserMedia filename:string --force-plural` with a clean environment
    Then the output should contain "test/factories/user_media.rb"
    And the file "test/factories/user_media.rb" should contain exactly:
      """
      FactoryBot.define do
        factory :user_media do
          filename { "MyString" }
        end
      end
      """

  Scenario: The factory_bot_rails generators add a factory in the correct spot
    When I write to "test/factories.rb" with:
      """
      FactoryBot.define do
      end
      """
    And I run `bundle exec rails generate model User name:string` with a clean environment
    And I run `bundle exec rails generate model Robot name:string` with a clean environment
    Then the file "test/factories.rb" should contain exactly:
      """
      FactoryBot.define do
        factory :robot do
          name { "MyString" }
        end

        factory :user do
          name { "MyString" }
        end

      end
      """

  Scenario: The factory_bot_rails generators does not create a factory file for each model if there is a factories.rb file in the test directory
    When I write to "test/factories.rb" with:
      """
      FactoryBot.define do
      end
      """
    And I run `bundle exec rails generate model User name:string` with a clean environment
    Then the file "test/factories/users.rb" should not contain "factory :user do"

  Scenario: The factory_bot_rails generators use a custom template
    When I write to "lib/templates/factory_bot/model/factories.erb" with:
      """
      <%= "Custom factory definition" %>
      """
    And I run `bundle exec rails generate model User` with a clean environment
    Then the file "test/factories/users.rb" should contain exactly:
      """
      Custom factory definition
      """

  Scenario: The factory_bot_rails generator can be disabled
    When I append to "config/application.rb" with:
      """
      Rails.application.configure do
        config.generators do |g|
          g.factory_bot false
        end
      end
      """
    And I run `bundle exec rails generate model User name:string age:integer` with a clean environment
    Then the output should not contain "test/factories/users.rb"
    And the output should contain "test/fixtures/users.yml"