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

15 commits

Author SHA1 Message Date
Emmanuel Gautier
0b53d28b4c
Pass index to block for *_list methods
When using one of the `*_list` methods, allow the block to receive array
index for each object:

```ruby
posts = build_list(:post, 10, title: 'Post number') do |post, i|
  post.title = "#{post.title} #{i + 1}"
end

posts.first.title # => "Post number 1"
posts.last.title  # => "Post number 10"
```

A block that only takes in the object still works as before, as does
leaving off the block entirely.

Co-authored-by: Mike Countis <mike.countis@gmail.com>
2020-04-24 11:36:17 -04: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
Daniel Colson
bf04aaa068 Autocorrect all static attributes to dynamic
Most of this was fixed by adding the `attribute-defined-statically-cop`
branch of `thoughtbot/rubocop-rspec` to the Gemfile and running:

```sh
  rubocop \
    --require rubocop-rspec \
    --only FactoryBot/AttributeDefinedStatically \
    --auto-correct
```

I had to update the cucumber tests manually, and I realized our changes
don't handle `ignore` blocks or blocks with arity 1 that use the yielded
DefinitionProxy. I will update
https://github.com/rubocop-hq/rubocop-rspec/pull/666to handle these
cases.
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
Tejas Bubane
8a65569f10 Add error message when count is missing for create_list
Closes #889

`create_list` was failing abruptly if the second argument was not
count. Give a descriptive error message for this case.
2016-08-26 08:40:56 -04:00
Ian Zabel
9610b38957 Deprecate #ignore in favor of #transient
Update documentation to reflect that transient is now preferred
2014-05-23 14:43:44 -04:00
Joshua Clayton
b095f24598 Convert to expect syntax 2013-01-18 13:58:36 -05:00
bbugh
488e42d77b Modified define_list_strategy_method to accept and use a block. 2012-10-22 08:15:47 -04:00
Josh Clayton and Jason Draper
bef5a01b31 Introduce new callback syntax
Instead of calling before_create, after_build, after_create, or
after_stub, you can now call:

    before(:create) {|instance| instance.name = "overridden!" }
    after(:create)  {|instance| instance.name = "overridden!" }
    after(:build)   {|instance| instance.name = "overridden!" }
    after(:stub)    {|instance| instance.name = "overridden!" }

Additionally, you can declare callbacks longhand:

    callback(:after_stub) {|instance| instance.name = "overridden!" }

This allows for custom callbacks to be defined:

    callback(:custom_callback) {|instance| instance.name = "overridden!" }

Which can then be used from a custom strategy:

    class CustomStrategy
      def association(runner); end

      def result(evaluation)
        evaluation.object.tap do |instance|
          evaluation.notify(:custom_callback, instance)
        end
      end
    end

    FactoryGirl.register_strategy(:custom, CustomStrategy)

This would allow for calling:

    FactoryGirl.custom(:user)

Which would return the user instance but execute the :custom_callback callback
on the user instance first.
2012-05-04 17:59:34 -04:00
Joshua Clayton
6c29b11477 Use 1.9 hash syntax 2012-03-16 16:43:23 -04:00
Joshua Clayton
9c6c252d0f Add tests and documentation for has_many relationships 2012-02-17 15:28:06 -05:00
Joshua Clayton
0b2c4da0a4 Move DefineConstant code into macro 2011-08-19 17:21:54 -04:00
Colin Gemmell
dd7aa22e0d Added create|build_list for easy generation of multiple factories at once
Closes #110
2011-06-28 16:02:05 -04:00