Created
March 26, 2015 14:40
-
-
Save caulfield/b2bf800e9427f447d0ca to your computer and use it in GitHub Desktop.
rspec subject with factorygirl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module FactoryGirlHelpers | |
# extract subject attributes and produce factory object from them | |
# | |
# @example subject_factory(:build_stubbed) | |
# @example subject_factory(:create, :comment_reply) | |
def subject_factory(method, factory_name = nil) | |
changed_attrbiutes = subject.changed | |
attributes = subject.attributes.slice changed_attrbiutes | |
factory_name ||= subject.class.to_s.parameterize | |
FactoryGirl.public_send method, factory_name, attributes | |
end | |
end | |
RSpec.configure do |config| | |
include FactoryGirlHelpers | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment