Skip to content

Instantly share code, notes, and snippets.

@norman
Last active Dec 22, 2015
Embed
What would you like to do?
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