Skip to content

Instantly share code, notes, and snippets.

@foca
Created September 10, 2010 16:31
Show Gist options
  • Save foca/573936 to your computer and use it in GitHub Desktop.
Save foca/573936 to your computer and use it in GitHub Desktop.
require "ffaker"
class ActiveRecord::Base
def self.define_factory(name=:factory, &block)
scope name, lambda { |overrides={}|
attributes = instance_eval(&block).merge(overrides)
where(attributes)
}
end
end
class User < ActiveRecord::Base
define_factory {{
:name => Faker::Name.name,
:email => Faker::Internet.email
}}
end
class UserTest < ActiveSupport::TestCase
test "name factory" do
user = User.factory(:name => "John Doe").new
assert_equal "John Doe", user.name
end
end
@oboxodo
Copy link

oboxodo commented Sep 10, 2010

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment