Skip to content

Instantly share code, notes, and snippets.

@norman
Last active December 22, 2015 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norman/6439554 to your computer and use it in GitHub Desktop.
Save norman/6439554 to your computer and use it in GitHub Desktop.
A very minimal alternative to FactoryGirl?
module FlexMinder
module Factories
def factory(klass, attributes)
name = klass.to_s
underscored = name.underscore
class_eval(<<-END, __FILE__, __LINE__ + 1)
def build_#{underscored}(attributes = {})
attributes = valid_#{underscored}_attributes.deep_merge(attributes)
#{name}.new(attributes)
end
def create_#{underscored}(attributes = {})
build_#{underscored}(attributes).tap {|x| x.save!}
end
def valid_#{underscored}_attributes
#{attributes.inspect}
end
END
end
end
end
ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'rr'
require 'flex_minder/factories'
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
include RR::Adapters::TestUnit
extend FlexMinder::Factories
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
# fixtures :all
# Add more helper methods to be used by all tests here...
factory Cpt, \
:code => 'foo',
:short_description => 'bar',
:long_description => 'baz'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment