1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00
Commit graph

11 commits

Author SHA1 Message Date
Daniel Colson
5f1a1de114 Run standardrb
This commit applies the changes from running `standardrb --fix`
2020-06-10 17:11:39 -04:00
Daniel Colson
975fc4ff29
Add functionality for enum traits (#1380)
## Enum traits

Given a Rails model with an enum attribute:

```rb
class Task < ActiveRecord::Base
  enum status: {queued: 0, started: 1, finished: 2}
end
```

It is common for people to define traits for each possible value of the enum:

```rb
FactoryBot.define do
  factory :task do
    trait :queued do
      status { :queued }
    end

    trait :started do
      status { :started }
    end

    trait :finished do
      status { :finished }
    end
  end
end
```

With this commit, those trait definitions are no longer necessary—they are defined automatically by factory_bot.

If automatically defining traits for enum attributes on every factory is not desired, it is possible to disable the feature by setting `FactoryBot.automatically_define_enum_traits = false` (see commit:  [Allow opting out of automatically defining traits](5a20017351)).

In that case, it is still possible to explicitly define traits for an enum attribute in a particular factory:

```rb
FactoryBot.automatically_define_enum_traits = false

FactoryBot.define do
  factory :task do
    traits_for_enum(:status)
  end
end
```

It is also possible to use this feature for other enumerable values, not specifically tied to ActiveRecord enum attributes:

```rb
class Task
  attr_accessor :status
end

FactoryBot.define do
  factory :task do
    traits_for_enum(:status, ["queued", "started", "finished"])
  end
end
```

The second argument here can be an enumerable object, including a Hash or Array.

Closes #1049 

Co-authored-by: Lance Johnson <lancejjohnson@gmail.com>
Co-authored-by: PoTa <pota@mosfet.hu>
Co-authored-by: Frida Casas <fridacasas.fc@gmail.com>
Co-authored-by: Daniel Colson <danieljamescolson@gmail.com>
2020-05-01 17:50:51 -04:00
Richie Thomas
ce2d9975f4
Refactor factory_spec.rb to conform to Let's Not style (#1345) 2020-01-17 11:26:30 -08:00
Alejandro Dustet
4d1cb6219b Deprecate and move to Internal factories methods
Why:
These methods are used internally for the functionality of the library
and are subject to change. Therefore shouldn't be part of the public
interface.

This PR:
- Moves the ```register_factory``` and ```factory_by_name``` methods to
the ```FactoryBot::Internal``` class.
- Deprecates the use of ```register_factory``` and ```factory_by_name```
from the ```FactoryBot``` module.
- Improve formatting of the specs
2019-06-04 19:39:04 -04:00
Daniel Colson
d18fde3062
Remove unhelpful spec
This spec was introduced back in 18c562a1e6, at which point it was
testing that the to_create block got passed to a proxy object.
A lot has changed since then, and this test is no longer testing
anything involving to_create (we can remove the call to to_create and it
still passes). It is testing that we instantiate a strategy and pass an
evaluation to it, but that isn't a helpful test. The behavior we
actually care about is well tested in acceptance specs.
2018-11-18 15:47:55 -05:00
Susan Wright
c22c9ab052 Rubocop: Fix Style/StringLiterals Offenses (#1216) 2018-10-07 21:45:51 -04:00
Hunter Braun
6a25e989b6 [Rubocop] Lint Cop Offenses (#1207) 2018-10-05 14:54:08 -04:00
Christian Bruckmayer
5b0257363c Add rubocop
to run rubocop locally and not to wait for HoundCI to bark at us.

The version is locked to 0.54 because this is the version HoundCI is using.
Using a newer version will not work as config changed from 0.54 to currently 0.59
and rubocop would complain with the old configuration.

thoughtbot uses the .rubocop.yml in thoughtbot/guides
as the orginizational config on Hound, so we inherit
from it so we can use the same config locally.

Fix #1195.
2018-09-18 22:58:56 -04:00
Daniel Colson
0473865b93 Remove static attributes
Co-authored-by: George Wambold <georgewambold@gmail.com>
2018-09-14 19:27:13 +00:00
Oli Peate
01d81f54b5 Remove unnecessary spec_helper requires
https://github.com/rspec/rspec/wiki#rspec
2018-05-21 18:03:28 +01:00
Avielle
c716ce01b4 Replace 'girl' with 'bot' everywhere (#1051)
Also: add a deprecation warning to factory_girl, asking users to switch to
factory_bot

https://github.com/thoughtbot/factory_girl/issues/921
2017-10-20 15:20:28 -04:00
Renamed from spec/factory_girl/factory_spec.rb (Browse further)