Skip to content

Instantly share code, notes, and snippets.

@caulfield
Created March 26, 2015 14:40
Show Gist options
  • Save caulfield/b2bf800e9427f447d0ca to your computer and use it in GitHub Desktop.
Save caulfield/b2bf800e9427f447d0ca to your computer and use it in GitHub Desktop.
rspec subject with factorygirl
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