modelfactory 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,139 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ # The classes below are rough mocks of ActiveRecord::Base that only handle instantiation.
4
+
5
+ # This class has defaults defined.
6
+ class ModelFactoryTestDef
7
+ def self.protected_attributes; nil; end
8
+ def self.accessible_attributes; nil; end
9
+
10
+ attr_accessor :id, :nest, :bar, :baz
11
+ def initialize(opts = {})
12
+ opts.each do |k,v|
13
+ self.send("#{k}=", v)
14
+ end
15
+ end
16
+ end
17
+
18
+ # This class has no defaults defined.
19
+ class ModelFactoryTestNoDef < ModelFactoryTestDef; end
20
+
21
+ # This class has nested defaults defined.
22
+ class ModelFactoryTestNestDef < ModelFactoryTestDef; end
23
+
24
+ # This class has protected attributes.
25
+ class ModelFactoryTestProtected < ModelFactoryTestDef
26
+ def self.protected_attributes; [:baz]; end
27
+
28
+ def initialize(opts = {})
29
+ opts = opts.clone
30
+ opts.delete(:baz) # Simulate a protected attribute.
31
+ super(opts)
32
+ end
33
+ end
34
+
35
+ # This class has accessible attributes.
36
+ class ModelFactoryTestAccessible < ModelFactoryTestDef
37
+ def self.accessible_attributes; [:id, :nest, 'bar']; end
38
+
39
+ def initialize(opts = {})
40
+ opts = opts.clone
41
+ opts.delete(:baz) # Simulate a protected attribute.
42
+ super(opts)
43
+ end
44
+ end
45
+
46
+ # This is the testfactory class def with test defaults.
47
+ class ModelFactoryTestFactory
48
+ extend ModelFactory
49
+ default ModelFactoryTestDef, { :bar => 'baz' }
50
+ default ModelFactoryTestNestDef, { :nest => default_model_factory_test_def }
51
+ end
52
+
53
+ class ModelFactoryTest < Test::Unit::TestCase
54
+ test "protected attributes" do
55
+ m = ModelFactoryTestFactory.new_model_factory_test_protected(:baz => 4)
56
+ assert_equal 4, m.baz
57
+ end
58
+
59
+ test "dynamic protected attributes" do
60
+ i = 1
61
+ m = ModelFactoryTestFactory.new_model_factory_test_protected(:bar => 3, :baz => lambda { i += 1 } )
62
+ assert_equal 2, m.baz
63
+ end
64
+
65
+ test "accessible attributes" do
66
+ m = ModelFactoryTestFactory.new_model_factory_test_accessible(:baz => 4)
67
+ assert_equal 4, m.baz
68
+ end
69
+
70
+ test "dynamic accessible attributes" do
71
+ i = 1
72
+ m = ModelFactoryTestFactory.new_model_factory_test_accessible(:bar => 3, :baz => lambda { i += 1 } )
73
+ assert_equal 2, m.baz
74
+ end
75
+
76
+ test "new with no arguments and nested defaults" do
77
+ m = ModelFactoryTestFactory.new_model_factory_test_nest_def
78
+ assert m.nest.id > 0
79
+ assert m.id > 0
80
+ end
81
+
82
+ test "new with arguments and nested defaults" do
83
+ m = ModelFactoryTestFactory.new_model_factory_test_nest_def(:bar => 'foo')
84
+ assert_equal 'foo', m.bar
85
+ assert m.nest.id > 0
86
+ assert m.id > 0
87
+ end
88
+
89
+ test "new with no arguments and explicit defaults" do
90
+ m = ModelFactoryTestFactory.new_model_factory_test_def
91
+ assert_equal 'baz', m.bar
92
+ assert m.id > 0
93
+ end
94
+
95
+ test "new with arguments and explicit defaults" do
96
+ m = ModelFactoryTestFactory.new_model_factory_test_def(:bar => 'foo')
97
+ assert_equal 'foo', m.bar
98
+ assert m.id > 0
99
+ end
100
+
101
+ test "create with arguments and explicit defaults" do
102
+ instance = ModelFactoryTestDef.new
103
+ ModelFactoryTestDef.expects(:create!).with(:bar => 'foo').returns(instance)
104
+ ModelFactoryTestFactory.create_model_factory_test_def(:bar => 'foo')
105
+ end
106
+
107
+ test "create with no arguments and explicit defaults" do
108
+ instance = ModelFactoryTestDef.new
109
+ ModelFactoryTestDef.expects(:create!).with(:bar => 'baz').returns(instance)
110
+ ModelFactoryTestFactory.create_model_factory_test_def
111
+ end
112
+
113
+ test "new with no arguments and no defaults" do
114
+ instance = ModelFactoryTestNoDef.new
115
+ ModelFactoryTestNoDef.expects(:new).returns(instance)
116
+ m = ModelFactoryTestFactory.new_model_factory_test_no_def
117
+ assert m.id > 0
118
+ end
119
+
120
+ test "new with arguments and no defaults" do
121
+ instance = ModelFactoryTestNoDef.new
122
+ ModelFactoryTestNoDef.expects(:new).with(:bar => 'foo').returns(instance)
123
+ m = ModelFactoryTestFactory.new_model_factory_test_no_def(:bar => 'foo')
124
+ assert m.id > 0
125
+ end
126
+
127
+ test "create with no arguments and no defaults" do
128
+ instance = ModelFactoryTestNoDef.new
129
+ ModelFactoryTestNoDef.expects(:create!).returns(instance)
130
+ ModelFactoryTestFactory.create_model_factory_test_no_def
131
+ end
132
+
133
+ test "create with arguments and no defaults" do
134
+ instance = ModelFactoryTestNoDef.new
135
+ ModelFactoryTestNoDef.expects(:create!).with(:bar => 'foo').returns(instance)
136
+ ModelFactoryTestFactory.create_model_factory_test_no_def(:bar => 'foo')
137
+ end
138
+ end
139
+
@@ -0,0 +1,12 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'mocha'
4
+ require File.dirname(__FILE__) + '/../lib/model_factory'
5
+
6
+ class << Test::Unit::TestCase
7
+ def test(name, &block)
8
+ test_name = "test_#{name.gsub(/[\s\W]/,'_')}"
9
+ raise ArgumentError, "#{test_name} is already defined" if self.instance_methods.include? test_name
10
+ define_method test_name, &block
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: modelfactory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.7.0
5
+ platform: ruby
6
+ authors:
7
+ - Justin Balthrop
8
+ - Zack Hobson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2008-11-13 00:00:00 -08:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: hoe
18
+ type: :development
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 1.8.2
25
+ version:
26
+ description: A replacement for fixtures.
27
+ email: justin@geni.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - License.txt
35
+ - Manifest.txt
36
+ - README.txt
37
+ files:
38
+ - History.txt
39
+ - License.txt
40
+ - Manifest.txt
41
+ - README.txt
42
+ - Rakefile
43
+ - lib/fixture_converter.rb
44
+ - lib/model_factory.rb
45
+ - lib/model_factory/version.rb
46
+ - script/destroy
47
+ - script/fixtures2factories
48
+ - script/generate
49
+ - script/txt2html
50
+ - setup.rb
51
+ - test/model_factory_test.rb
52
+ - test/test_helper.rb
53
+ has_rdoc: true
54
+ homepage: http://modelfactory.rubyforge.org/
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --main
58
+ - README.txt
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ requirements: []
74
+
75
+ rubyforge_project: modelfactory
76
+ rubygems_version: 1.2.0
77
+ signing_key:
78
+ specification_version: 2
79
+ summary: A replacement for fixtures.
80
+ test_files:
81
+ - test/model_factory_test.rb