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

475 commits

Author SHA1 Message Date
Joshua Clayton
6a692fe711 Add FactoryGirl.lint to ensure all factories are valid
This commit adds FactoryGirl.lint, which iterates over each defined
factory, builds it, and ensures it is valid. If any factories are
invalid, an exception is raised and includes the names of each invalid
factory.
2014-02-07 22:21:08 -05:00
Joshua Clayton
ed598ec54c Update repo to follow thoughtbot guide for whitespace in blocks 2013-12-14 22:33:15 -05:00
Potapov Sergey
664c426bd0 Fix memory leak: do not add trait duplications to @defined_traits
Closes 
2013-12-13 15:12:48 -05:00
Joshua Clayton
70de122435 Bump version to 4.3.0 2013-11-03 22:21:04 -05:00
Andy Waite and Josh Clayton
b9e1dde7e8 Add *_pair methods to create only two records
This introduces a set of methods for each build strategy where only two
records are created. Because the *_list methods can create an arbitrary
number (often too high), this introduces *_pair to ensure only two
records are created (and the number 2 doesn't need to be specified in
the call).
2013-11-01 17:32:50 -04:00
Fumiaki MATSUSHIMA
69a6c9e14f Fix factory named camel case string 2013-10-08 09:01:45 -04:00
Joshua Clayton
e9c23dc817 Use instance_exec passing self as an argument for dynamic attributes
Calling `block.call(self)` means any implied scope of block evaluation
goes out the window when an arity of 1 is used. This change uses
`instance_exec`, passing along self as the argument for the block
variable, meaning that the block is still invoked within the context of
the FactoryGirl::Evaluator while allowing the variable to have access to
the evaluator.

Closes 
2013-06-07 10:42:50 -04:00
Joshua Clayton
fa9a81c166 Raise if a method is defined with a FactoryGirl block (factory or trait)
This resolves an issue where users have defined methods inside factory
girl blocks. This hasn't worked in quite a while and without raising,
users would be able to define methods and see an odd error (a
NoMethodError for `singleton_method_added=`). Instead, we raise
immediately so that no issues come up later when they actually try to
use the factories.

Closes 
2013-04-26 16:10:19 -04:00
Jan Bernacki
09481eebef Allow use of Symbol#to_proc in FactoryGirl callbacks
This allows callbacks to be called as such:

    FactoryGirl.define do
      factory :user do
        after :create, &:confirm!
      end
    end

Closes 
2013-04-05 10:14:29 -04:00
Joshua Clayton
36cb43e9b0 Add global callbacks
This allows callbacks (after :build, :create, etc.) to be defined at the
FactoryGirl level; this means that the callback will be invoked for all
factories. This is primarily to maintain consistency and follow the
principle of least surprise.

As usual, callbacks are applied from the lowest component to the
highest, meaning that global callbacks will be run after factory and
trait callbacks are run.

    FactoryGirl.define do
      after(:build) {|object| puts "Built #{object}" }

      factory :user
      # ...
    end

Closes 
Closes 
2013-02-08 11:24:35 -05:00
Joshua Clayton
21ba127827 Bump version to 4.2.0 2013-01-18 16:13:08 -05:00
Joshua Clayton
18a512576f Fix warnings
[Closes ]
2013-01-18 13:58:14 -05:00
Derek Prior and Josh Clayton
bf29843d15 Evaluate sequences within the context of the Evaluator when possible
This fixes weird issues where methods invoked within sequences
(like `sprintf`) fail because these methods are being evaluated within
the context of the DefinitionProxy. With this change, invoking `#next`
on a sequence happens from the evaluator so if the scope is provided (it
usually will be), it'll run in the proper context. This means a couple
of oddities are fixed:

1. Developers can now refer to methods on the object instance, just like
in dynamic attributes:

    class User
      def company
        # company lookup
      end
    end

    FactoryGirl.define do
      factory :user do
        sequence(:job_title) {|n| "{title} #{n} at #{company.name}" }
      end
    end

2. Invoke methods typically available because the method is available on
Object (e.g. Kernel methods):

    FactoryGirl.define do
      factory :user do
        sequence(:last_4_ssn) {|n| sprintf '%04d', n }
      end
    end

[]
2013-01-18 09:33:54 -05:00
Ari Pollak
af282a178b Match build_stubbed's created_at type to ActiveRecord
Closes 
2012-12-21 13:09:32 -08:00
Joshua Clayton
e85c0086aa Allow setting id for build_stubbed objects
Closes 
2012-11-07 15:44:51 -05:00
bbugh
488e42d77b Modified define_list_strategy_method to accept and use a block. 2012-10-22 08:15:47 -04:00
Joshua Clayton
82208a1008 Simplify #build_hierarchy 2012-09-14 08:22:28 -04:00
Joshua Clayton
e3e9ce6968 Bump version to 4.1.0 2012-09-11 11:21:09 -04:00
Joshua Clayton
f92195f35a Support binding a block to multiple callbacks
This allows for binding multiple callbacks (after_stub, before_create,
etc.) to a single block. This is useful if you want a block to be called
across all build strategies (since build_stubbed doesn't share any
callbacks with build/create).

Examples:

    factory :user do
      callback(:after_stub, :before_create) { do_something }
      after(:stub, :create) { do_something_else }
      before(:create, :custom) { do_a_third_thing }
    end
2012-09-11 11:14:26 -04:00
Joshua Clayton
ed7c86cfed Bump version to 4.0.0 2012-08-03 17:00:06 -04:00
Joshua Clayton
86a20fb427 Bump version to 4.0.0.rc1 2012-08-02 11:23:25 -04:00
Joshua Clayton
e9d9e3099b Prep for FactoryGirl 4.0 2012-08-02 11:17:39 -04:00
Joshua Clayton
8d2f517f94 Bump version to 3.6.1 2012-08-02 11:16:39 -04:00
Joshua Clayton
37fe26315e Stub update_column to raise, which will replace update_attribute in Rails 4 2012-08-02 11:06:18 -04:00
Joshua Clayton
c9ed3d88e5 Improve ability to define associations with traits and add documentation 2012-08-02 10:19:17 -04:00
Joshua Clayton
a7fff84db4 Bump version to 3.6.0 2012-07-27 13:00:24 -04:00
Joshua Clayton
e038bf8eec Add respond_to_missing? on NullObject for 1.9 compatability 2012-07-27 10:14:57 -04:00
Joshua Clayton
acb2636321 Memoize the names of attributes to assign for a 33% speed increase on factories with overrides 2012-07-25 09:13:25 -04:00
Joshua Clayton
8209a461ee Remove dependency on build_class in Evaluator 2012-07-24 18:50:11 -04:00
Joshua Clayton
0c165ec993 Add DefinitionHierarchy to handle inheritance of some definition attributes
This includes #callbacks, #constructor, and #to_create. The reasoning
behind this is that we were mimicing an inheritance chain via methods;
now, we actually generate classes, which Factory maintains, who inherit
from their parent's hierarchy. We build the hierarchy during compilation
to conditionally define methods based on whether what we're dealing
with from the definition is actually meaningful. The base class
(DefinitionHierarchy) uses the defaults (an empty array for #callbacks
and the global #to_create and #constructor) so once we hit the top
level, if the definition doesn't set any overrides, we have a list of
sensible values.
2012-07-11 23:05:16 -04:00
Joshua Clayton
4e2b7e0b3f Allow factories with traits to be used in associations
This allows defining associations with factories using traits by slicing
up the hash passed to association. Passing an array to `:factory` will
use the first item in the array as the factory, with any subsequent
symbols as traits.

Here's an example:

    factory :post do
      association :author, factory: [:user, :admin], name: 'John Doe'
    end

Closes 
2012-06-29 11:06:08 -04:00
Joshua Clayton
5e90ff191a Follow pattern for raising when instantiating 2012-06-25 17:59:48 -04:00
Joshua Clayton
71435ee24c Bump version to 3.5.0 2012-06-22 17:00:29 -04:00
Joshua Clayton
4bccbb90d1 Allow created_at to be set when using build_stubbed
Closes 
2012-06-22 14:33:37 -04:00
Joshua Clayton
154346b01b Deprecate FactoryGirl step definitions 2012-06-22 13:51:33 -04:00
Joshua Clayton
13fcdaf440 Bump version to 3.4.2 2012-06-19 22:28:24 -04:00
Joshua Clayton
8f3cd52e0d Ensure that callbacks are triggered once for implicit traits
Closes 
2012-06-19 22:25:46 -04:00
Joshua Clayton
1b37231b67 Bump version to 3.4.1 2012-06-18 09:10:00 -04:00
Joshua Clayton
779eafccbd Fix various trait bugs so traits can be used within each other
The previous implementation of trait handling within the Definition
didn't account for when implicit traits were used within other traits.
This is useful if you have two different traits, but one depends on
another; for example, a refunded order and a completed order could both
have the attribute `completed_at` set, but refunded would additionally
have `refunded_at` set:

    FactoryGirl.define do
      factory :order do
        trait :completed do
          completed_at { 3.days.ago }
        end

        trait :refunded do
          completed
          refunded_at { 1.day.ago }
        end
      end
    end

This also tests to make sure that callbacks, custom constructors, and
creation overrides work correctly when implicit traits are used within
other traits.

Fixes 
2012-06-15 20:08:42 -04:00
Joshua Clayton
9a72ea2377 Don't mutate hash 2012-06-12 09:07:08 -04:00
Joshua Clayton
c7c5cbd5db Bump version to 3.4.0 2012-06-11 08:11:42 -04:00
Joshua Clayton
f83c27e15d Support refering to factory based on class name
Allows for build/creating based on RSpec's described_class, useful since
we've deprecated other syntaxes from FG.

Closes 
2012-06-08 23:54:22 -04:00
Joshua Clayton
e8d32b9254 Move DisallowsDuplicatesRegistry to a decorator 2012-06-08 17:25:18 -04:00
Joshua Clayton
9bc8e50b44 Refer to the correct attribute 2012-06-08 17:06:19 -04:00
Joshua Clayton
2b50190a07 Make hash of public attributes available within the initialize_with block
Closes 
2012-06-08 17:05:36 -04:00
Joshua Clayton
775dc8bd88 Fix namespace issues in 1.9.2 2012-06-08 12:17:21 -04:00
Joshua Clayton
a5b3a97c9d Optionally disable duplicate assignment of attributes in initialize_with
By setting:

    FactoryGirl.duplicate_attribute_assignment_from_initialize_with =
false

This turns off duplicate assignment of attributes accessed from
initialize_with. This means any attributes accessed from the factory and
assigned in the initialize_with block won't be subsequently set after
the object has been instantiated.

This will be the default functionality in 4.0.

Closes 
2012-05-18 17:38:23 -04:00
Joshua Clayton
64e50a3121 Remove unnecessary conditional in Evaluator#method_missing
Evaluator has all methods defined dynamically; this includes standard
attributes AND overrides. Because of this, method_missing never actually
gets used to retrieve a value from previously declared attributes or
overrides.

This branch of logic was never accessed
2012-05-18 11:51:59 -04:00
Joshua Clayton
7b382214b3 Sequences support Enumerators
This introduces an EnumeratorAdapter so everything coming in looks like
an Enumerator. It doesn't use the adapter if the #peek method exists on
the object.

This allows for sequences like:

    sequence(:cities, %w[Boston Atlanta Detroit Seattle].cycle)

This also makes it easier to cycle through arrays and ranges, since they
can be converted to Enumerators rather easily.

    sequence(:month, (1..12).to_enum)
    sequence(:month, %w[foo bar baz].to_enum)

This doesn't handle when calling Range#step out of the box, because
Ruby returns an Enumerator but ActiveSupport 3.x returns an array,
meaning #to_enum still needs to be called.

Closes , 
2012-05-13 01:54:41 -04:00
Joshua Clayton
d0e56fee9c Bump version to 3.3.0 2012-05-13 00:39:57 -04:00