This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Tests for builders

  • Loading branch information...
vinibaggio committed Mar 3, 2011
1 parent 544a99b commit f03edd53de1ccbf4bd3330096852edb736868a11
Showing with 151 additions and 11 deletions.
  1. +25 −6 lib/hermes/builders.rb
  2. +10 −0 test/builders.rb
  3. +4 −0 test/fixtures/users.yml
  4. +101 −0 test/hermes/builders_test.rb
  5. +5 −5 test/support/models.rb
  6. +6 −0 test/test_helper.rb
View
@@ -1,6 +1,6 @@
# Rails developers have had bad experience with fixtures since a long time
# due to several reasons, including misuse.
#
#
# This misuse of fixtures is often characterized by a huge ammount of
# fixtures, causing a lot of data to maintain and dependence between tests.
# In my experience working (and rescueing) different applications, 80% of
@@ -11,7 +11,7 @@
# we need a huge amount of data in this test which we usually don't need in
# order tests.
#
# For such scenarios, factories are a fine solution. They won't cluster
# For such scenarios, factories are a fine solution. They won't clutter
# all your database since they are created for these specific tests and they
# are also easier to maintain.
#
@@ -79,6 +79,9 @@
#
module Hermes
module Builders
ATTRIBUTES_REGEX = /valid_(.*?)_attributes$/
BUILDER_REGEX = /(create|new)_(.*?)(!)?$/
@@builders = ActiveSupport::OrderedHash.new
def self.build(name, options={}, &block)
@@ -96,23 +99,39 @@ def self.build(name, options={}, &block)
def self.retrieve(scope, name, method, options)
if builder = @@builders[name.to_sym]
klass, block = builder
hash = block.bind(scope).call.merge(options || {})
hash.delete("id")
hash = block.bind(scope).call
hash.symbolize_keys!
hash.merge!(options || {})
hash.delete(:id)
[klass, hash]
else
raise NoMethodError, "No builder #{name.inspect} for `#{method}'"
end
end
def respond_to?(method)
case method.to_s
when BUILDER_REGEX
true
when ATTRIBUTES_REGEX
true
else
super
end
end
def method_missing(method, *args, &block)
case method.to_s
when /(create|new)_(.*?)(!)?$/
when BUILDER_REGEX
klass, hash = Builders.retrieve(self, $2, method, args.first)
object = klass.new
object.send("attributes=", hash, false)
object.send("save#{$3}") if $1 == "create"
object
when /valid_(.*?)_attributes$/
when ATTRIBUTES_REGEX
Builders.retrieve(self, $1, method, args.first)[1]
else
super
View
@@ -0,0 +1,10 @@
module Hermes::Builders
build :user, :like => :the_user do
{}
end
build :another_user, :class => User do
{:name => 'Another user', :active_at => Time.parse("Thu, 03 Mar 2011 18:11:26 UTC +00:00") }
end
end
View
@@ -0,0 +1,4 @@
the_user:
name: The User
active_at: Thu, 03 Mar 2011 18:11:26 UTC +00:00
@@ -0,0 +1,101 @@
require 'test_helper'
class BuildersTest < Hermes::IntegrationCase
FIXTURE_DATE = Time.parse("Thu, 03 Mar 2011 18:11:26 UTC +00:00").utc
fixtures :users
context "building models based on fixtures" do
test 'creating a model' do
user = create_user
assert_equal "The User", user.name
assert_equal FIXTURE_DATE, user.active_at
assert user.persisted?
end
test 'creating a model with bang!' do
user = create_user!
assert_equal "The User", user.name
assert_equal FIXTURE_DATE, user.active_at
assert user.persisted?
end
test 'failing to create a model with bang! and invalid attributes' do
assert_raise ActiveRecord::RecordInvalid do
create_user!(:name => nil)
end
end
test 'do not raise exception when bang is used but not being saved' do
assert_nothing_raised do
new_user!(:name => nil)
end
end
test 'building a model' do
user = new_user
assert_equal "The User", user.name
assert_equal FIXTURE_DATE, user.active_at
assert user.new_record?
end
test 'generating a hash of attributes' do
user_attributes = valid_user_attributes
assert_equal 'The User', user_attributes[:name]
assert_equal FIXTURE_DATE, user_attributes[:active_at]
end
test 'respond_to is valid for attributes' do
assert respond_to?(:valid_user_attributes)
end
test 'respond_to is valid for builder' do
assert respond_to?(:create_user)
end
end
context "building new models using a specified class" do
test 'create an instance of user' do
another_user = new_another_user
assert_equal 'Another user', another_user.name
assert another_user.kind_of?(User)
end
end
context "building new models" do
test 'load a model' do
user = create_another_user
assert_equal 'Another user', user.name
assert_equal FIXTURE_DATE, user.active_at
end
test 'getting its attributes' do
user = valid_another_user_attributes
assert_equal 'Another user', user[:name]
assert_equal FIXTURE_DATE, user[:active_at]
end
test 'respond_to is valid for attributes' do
assert respond_to?(:valid_another_user_attributes)
end
test 'respond_to is valid for builder' do
assert respond_to?(:create_another_user)
end
test 'respond_to is valid for builder with bang' do
assert respond_to?(:create_another_user!)
end
test 'respond_to is valid for builder with new' do
assert respond_to?(:new_another_user)
end
end
end
View
@@ -1,10 +1,10 @@
module Support
module Models
def create_user(options={})
User.create!({
:name => 'The User', :active_at => Time.now
}.merge(options))
end
# def create_user(options={})
# User.create!({
# :name => 'The User', :active_at => Time.now
# }.merge(options))
# end
end
end
View
@@ -21,10 +21,16 @@
require 'active_support/test_case'
require 'hermes'
require 'hermes/actions'
require 'hermes/builders'
require 'hermes/scopes'
require 'hermes/context'
require 'hermes/integration_case'
# Support
require 'support/models'
require 'support/assertions'
# Builders
require 'builders'
ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures/", __FILE__)

0 comments on commit f03edd5

Please sign in to comment.