factory_bot
factory_bot copied to clipboard
Context helpers for callbacks:
Fixes: #1694 Fixes: #1649
Context is provided as a block parameter in callbacks. This pull requests adds some helper methods to context, making it easier to detect the strategy used and identify which attributes were defined by the user / factory.
Methods included are:
- :stategy
- :defined_attributes
- :user_defined_attributes
- :factory_defined_attributes
All can be queried:
context.strategy #=> 'build'
context.strategy.build? #=> true
context.user_defined_attributes #=> [:age, :name]
context.user_defined_attributes.age? #=> true
context.user_defined_attributes.height? #=> false
Although the Evaluator class is marked @api private, being included as a callback parameter means users are already accessing it. Making it easier to use can only be a good thing.
Tests and documentation added.