Browse files

Updated to allow declaration of dummy methods in a separate models/du…

…mmy/{model}.rb file
  • Loading branch information...
1 parent 4ad464f commit 206d2cc173dea92e30721fbc7243a1759ae9d065 @tadman tadman committed Oct 6, 2010
Showing with 49 additions and 9 deletions.
  1. +2 −0 .gitignore
  2. +1 −1 VERSION
  3. +18 −1 lib/test_dummy.rb
  4. +5 −1 lib/test_dummy/railtie.rb
  5. +14 −1 test/helper.rb
  6. +2 −2 test/models/simple.rb
  7. +5 −1 test/test_test_dummy.rb
  8. +2 −2 test_dummy.gemspec
View
2 .gitignore
@@ -5,6 +5,8 @@
*.tmproj
tmtags
+test/log
+
*~
\#*
.\#*
View
2 VERSION
@@ -1 +1 @@
-0.1.4
+0.2.0
View
19 lib/test_dummy.rb
@@ -9,6 +9,10 @@ def self.included(base)
base.send(:include, InstanceMethods)
end
+ def self.declare(on_class, &block)
+ on_class.instance_eval(&block)
+ end
+
module Support
# Combines several sets of parameters together into a single set in order
# of lowest priority to highest priority. Supplied list can contain nil
@@ -118,6 +122,19 @@ def can_dummy?(*names)
# the dummy operation is completed. Returns a dummy model which has not
# been saved.
def build_dummy(with_attributes = nil)
+ unless (defined?(@_dummy_module))
+ @_dummy_module =
+ begin
+ dummy_path = File.expand_path("models/dummy/#{name.underscore}.rb", Rails.root)
+
+ if (File.exist?(dummy_path))
+ require dummy_path
+ end
+ rescue LoadError
+ false
+ end
+ end
+
model = new(TestDummy::Support.combine_attributes(scoped.scope_for_create, with_attributes))
yield(model) if (block_given?)
@@ -211,7 +228,7 @@ def dummy_method_call(model, with_attributes, block)
when 1
block.call(model)
else
- block.call
+ model.instance_eval(&block)
end
end
View
6 lib/test_dummy/railtie.rb
@@ -8,12 +8,16 @@
end
else
class TestDummy::Railtie < Rails::Railtie
- config.before_configuration do
+ def self.apply!
if (defined?(ActiveRecord))
ActiveRecord::Base.send(:include, TestDummy)
end
ActiveSupport::TestCase.send(:include, TestDummy::TestHelper)
end
+
+ config.before_configuration do
+ apply!
+ end
end
end
View
15 test/helper.rb
@@ -1,14 +1,27 @@
require 'rubygems'
require 'test/unit'
+ENV['RAILS_ENV'] = 'test'
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
gem 'rails'
require 'rails'
-gem 'activerecord'
+require 'active_record'
require 'test_dummy'
+TestDummy::Railtie.apply!
+
class Test::Unit::TestCase
end
+
+module TestDummy
+ class Application < Rails::Application
+ config.active_support.deprecation = :warn
+ end
+end
+
+TestDummy::Application.initialize!
+ActiveRecord::Base.establish_connection(Rails.env)
View
4 test/models/simple.rb
@@ -1,5 +1,5 @@
-class Simple
- to_dummy :name do
+class Simple < ActiveRecord::Base
+ dummy :name do
TestDummy::Helper.random_string(8)
end
end
View
6 test/test_test_dummy.rb
@@ -1,8 +1,12 @@
require 'helper'
+require 'models/simple'
+
class TestTestDummy < Test::Unit::TestCase
def test_simple_model
- simple = Simple.create_fake
+ simple = Simple.dummy
+
+ assert_equal Simple, simple.class
assert_equal [ ], simple.errors.full_messages
assert !simple.new_record?
View
4 test_dummy.gemspec
@@ -5,11 +5,11 @@
Gem::Specification.new do |s|
s.name = %q{test_dummy}
- s.version = "0.1.4"
+ s.version = "0.2.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["tadman"]
- s.date = %q{2010-09-03}
+ s.date = %q{2010-10-06}
s.description = %q{Test Dummy allows you to define how to fake models automatically so that you can use dummy data for testing instead of fixtures. Dummy models are always generated using the current schema and don't need to me migrated like fixtures.}
s.email = %q{github@tadman.ca}
s.extra_rdoc_files = [

0 comments on commit 206d2cc

Please sign in to comment.