Last active
December 22, 2015 07:39
-
-
Save norman/6439554 to your computer and use it in GitHub Desktop.
A very minimal alternative to FactoryGirl?
This file contains hidden or 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 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 |
This file contains hidden or 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
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