object-factory 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ = Version 0.2.3 (2009-11-04)
2
+
3
+ Bugfix when creating default values for a model.
4
+
5
+ = Version 0.2.0 (2009-05-20)
6
+
7
+ Added clean_up options
8
+
9
+ = Version 0.1.4 (2009-03-25)
10
+
11
+ Added a_number to allow the generation of unique integers
12
+
13
+ = Version 0.1.3 (2008-12-08)
14
+
15
+ Added generate_ip_address
16
+
17
+ = Version 0.1.2 (2008-12-08)
18
+
19
+ ...
20
+
21
+ = Version 0.1.1 2008-11-24
22
+
23
+ Added when_creating_a and when_creating_an as short-cut methods
24
+
25
+ = Version 0.1.0 (2008-11-24)
26
+
27
+ Initial Release
@@ -0,0 +1,11 @@
1
+ CHANGELOG
2
+ Manifest
3
+ README.rdoc
4
+ Rakefile
5
+ github.rb
6
+ init.rb
7
+ lib/object_factory.rb
8
+ object-factory.gemspec
9
+ spec/object_spec.rb
10
+ spec/spec.opts
11
+ tasks/rspec.rake
@@ -0,0 +1,69 @@
1
+ = Object-factory
2
+
3
+ A ruby gem designed to make it simple to create test objects within your test framework, so you don't need to use nasty fixtures.
4
+
5
+ If you don't use the a_saved method then it should work with any type of model, if you do use a_saved then it will work with any model that has a save method (that returns false on failure - so ActiveRecord and DataMapper should be OK).
6
+
7
+ == Install
8
+
9
+ gem install brightbox-object-factory --source http://gems.github.com/
10
+
11
+ == Usage
12
+
13
+ See the wiki page at http://github.com/brightbox/object-factory/wikis/home for the latest docs. In particular, http://wiki.github.com/brightbox/object-factory/usage-with-rails-rspec-and-cucumber is a pretty good example of how to get started.
14
+
15
+ However, it works something like this:
16
+
17
+ when_creating_a Person, :auto_generate => :employee_code
18
+
19
+ @person = a Person, :first_name => 'John', :last_name => 'Smith'
20
+ puts @person.employee_code # will show a unique auto-generated value
21
+
22
+ And your options are:
23
+
24
+ * :auto_generate => [:field1, :field2]: generates a unique string value for the given field name or array of field names
25
+ * :auto_confirm => :password: generates a unique string value for the given field name or array of field names and also sets field_name_confirmation to the same value
26
+ * :generate_email_address => :email: generates a randomised email address for the given field name or array of field names
27
+ * :generate_ip_address => :ip: generates a randomised ip address for the given field name or array of field names
28
+ * :set => { :field3 => 'value3', :field4 => 'value4' }: sets the given fields to the supplied static values
29
+ * :generate => { :field5 => lambda { Date.today } }: sets the given fields to the supplied dynamic values
30
+
31
+ If you want to generate a unique value yourself, you can use Object::Factory's next_number call (which has a simple shortcut a_number).
32
+
33
+ :generate => { :field => "unique-value-#{a_number}" }
34
+
35
+ == Cleaning up
36
+
37
+ (currently only for ActiveRecord)
38
+
39
+ If you register your (ActiveRecord) class with the clean_up option, then the factory can ensure that all instances are deleted. This is useful if you cannot use transactional_fixtures (for example using Watir or Culerity in your test suite).
40
+
41
+ Add a clean_up parameter when registering your classes:
42
+
43
+ when_creating_a Person, :auto_generate => [:first_name, :last_name], :clean_up => true
44
+
45
+ Then in your "after" section call:
46
+
47
+ Object.factory.clean_up
48
+
49
+ This will call delete_all on any registered classes (hence it is currently ActiveRecord only).
50
+
51
+ == Rails
52
+
53
+ To use this with rails, stick the following in your +environment.rb+:
54
+
55
+ config.gem "object-factory"
56
+
57
+ Also check out http://wiki.github.com/brightbox/object-factory/usage-with-rails-rspec-and-cucumber for more information on integrating Object-Factory with your application.
58
+
59
+ == Released under the MIT Licence
60
+
61
+ Copyright (c) 2008 Brightbox Systems Ltd
62
+
63
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
64
+
65
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
66
+
67
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
68
+
69
+ See http://www.brightbox.co.uk/ for contact details.
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('object-factory', '0.2.3') do | config |
6
+ config.description = 'A simple object factory to help you build valid objects in your tests'
7
+ config.url = 'http://github.com/brightbox/object-factory'
8
+ config.author = 'Brightbox Systems Ltd'
9
+ config.email = 'hello@brightbox.co.uk'
10
+ config.ignore_pattern = ['tmp/*', 'script/*']
11
+ config.dependencies = ['brightbox-rujitsu']
12
+ config.development_dependencies = []
13
+ end
14
+
15
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each do | rake_file |
16
+ load rake_file
17
+ end
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+ require 'yaml'
3
+
4
+ if ARGV.size < 1
5
+ puts "Usage: github-test.rb my-project.gemspec"
6
+ exit
7
+ end
8
+
9
+ require 'rubygems/specification'
10
+ data = File.read(ARGV[0])
11
+ spec = nil
12
+
13
+ if data !~ %r{!ruby/object:Gem::Specification}
14
+ Thread.new { spec = eval("$SAFE = 3\n#{data}") }.join
15
+ else
16
+ spec = YAML.load(data)
17
+ end
18
+
19
+ spec.validate
20
+
21
+ puts spec
22
+ puts "OK"
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ # placeholder to make this a rails plugin
@@ -0,0 +1,224 @@
1
+ require 'rubygems'
2
+ require 'rujitsu'
3
+
4
+ class Object
5
+ # return an instance of an Object::Factory
6
+ def self.factory
7
+ THE_OBJECT_FACTORY_INSTANCE
8
+ end
9
+
10
+ # Factory allows test suites to build new instances of objects, specifying some simple constraints on certain fields
11
+ # If a new instance is created via the factory then that instance can have specialist values automatically applied to given fields, meaning that it should be possible for test cases to build valid objects without having to specify a full valid field-set
12
+ # The factory should not be created directly, but instead accessed through the Object#factory method.
13
+ # Expected usage:
14
+ # Object.factory.configure Person, :auto_generate => [:email, :telephone], :auto_confirm => :password
15
+ # instance = a Person
16
+ # instance will have a unique value for :email and :telephone and will ensure that :password and :password_confirmation have the same value.
17
+ class Factory
18
+ attr_accessor :generator
19
+
20
+ def initialize
21
+ reset
22
+ end
23
+
24
+ # Set this factory back to its pristine state, with no objects configured
25
+ def reset
26
+ @confirmed_fields = {}
27
+ @generators = {}
28
+ @classes_for_clean_up = []
29
+ end
30
+
31
+ # clean up all instances
32
+ def clean_up
33
+ @classes_for_clean_up.each do | klass |
34
+ klass.delete_all
35
+ end
36
+ end
37
+
38
+ # Create a new instance of the given class with the given parameters and apply the auto-generated fields, according to the configured rules
39
+ def create_a klass, parameters = {}
40
+ instance = klass.new parameters
41
+
42
+ generate_confirmations_for instance, parameters
43
+ generate_values_for instance, parameters
44
+
45
+ return instance
46
+ end
47
+
48
+ # Create a new instance of the given class with the given parameters, auto-generate the field values and then call save!
49
+ def create_and_save_a klass, parameters = {}
50
+ instance = create_a klass, parameters
51
+ raise CannotSaveError, instance.errors.inspect unless instance.save
52
+ return instance
53
+ end
54
+
55
+ # Set up the required auto-generated fields for the given class.
56
+ # Object.factory.when_creating_a MyClass, :auto_generate => [:field1, :field2], :auto_confirm => :password, :generate_email_address => :email_address, :set => { :field3 => 'value3', :field4 => 'value4' }, :generator => { :field5 => lambda {Date.today}, :field6 => lambda {Time.now} }
57
+ # Options are:
58
+ # * :auto_generate specifies a field name or array of field names that are to have unique string values assigned to them
59
+ # * :auto_confirm specifies a field name or array of field names that are to be set to a unique value; with the same value being assigned to field_name_confirmation
60
+ # * :generate_email_address specifies a field name or array of field names that are set to be randomised email addresses
61
+ # * :set specifies a Hash of field names and fixed values
62
+ # * :generate specifies a Hash of field names and lambdas that are used to generate a dynamic value
63
+ def when_creating_a klass, options = {}
64
+ need_to_generate_values_for klass, options[:auto_generate] unless options[:auto_generate].nil?
65
+ need_to_confirm_values_for klass, options[:auto_confirm] unless options[:auto_confirm].nil?
66
+ need_to_generate_email_addresses_for klass, options[:generate_email_address] unless options[:generate_email_address].nil?
67
+ need_to_generate_ip_addresses_for klass, options[:generate_ip_address] unless options[:generate_ip_address].nil?
68
+ need_to_set_values_for klass, options[:set] unless options[:set].nil?
69
+ need_to_set_generators_for klass, options[:generate] unless options[:generate].nil?
70
+ register_for_clean_up klass if options[:clean_up]
71
+ end
72
+
73
+ alias :when_creating_an :when_creating_a
74
+ alias :create_and_save_an :create_and_save_a
75
+
76
+ # An Object::Factory::ValueGenerator that is used to actually build the unique values used to populate the required fields
77
+ def generator
78
+ @generator ||= ValueGenerator.new
79
+ end
80
+
81
+ # Generate a unique Integer
82
+ def next_number
83
+ generator.unique_integer
84
+ end
85
+
86
+ # A simple class that generates unique values
87
+ class ValueGenerator
88
+ def initialize
89
+ @counter = 0
90
+ end
91
+
92
+ def unique_integer
93
+ @counter += 1
94
+ end
95
+
96
+ def value_for klass, field
97
+ "#{klass.name.to_s}-#{field.to_s}-#{unique_integer}"
98
+ end
99
+ end
100
+
101
+ # Error raised when create_and_save_a cannot save the object
102
+ class CannotSaveError < RuntimeError; end
103
+
104
+ # print the rules for a given class
105
+ def print_configuration_for klass
106
+ fields_and_generators = @generators[symbol_for(klass)]
107
+ unless fields_and_generators.nil?
108
+ fields_and_generators.each do | field_name, generator |
109
+ puts "#{field_name} uses a lambda"
110
+ end
111
+ end
112
+ end
113
+
114
+ private
115
+
116
+ def symbol_for object
117
+ klass = object.is_a?(Class) ? object : object.class
118
+ return klass.name.to_sym
119
+ end
120
+
121
+ def need_to_generate_values_for klass, fields
122
+ fields = [fields] unless fields.respond_to?(:each)
123
+ fields.each do | field |
124
+ add_generator_for klass, field, lambda { generator.value_for(klass, field) }
125
+ end
126
+ end
127
+
128
+ def need_to_confirm_values_for klass, fields
129
+ fields = [fields] unless fields.respond_to?(:each)
130
+ @confirmed_fields[symbol_for(klass)] = fields
131
+ end
132
+
133
+ def need_to_generate_email_addresses_for klass, fields
134
+ fields = [fields] unless fields.respond_to?(:each)
135
+ fields.each do | field |
136
+ add_generator_for klass, field, lambda { 6.random_letters + '@' + 10.random_letters + '.com' }
137
+ end
138
+ end
139
+
140
+ def need_to_generate_ip_addresses_for klass, fields
141
+ fields = [fields] unless fields.respond_to?(:each)
142
+ fields.each do | field |
143
+ add_generator_for klass, field, lambda {
144
+ octs = []
145
+ 4.times { octs << 1.random_numbers(:to => 255) }
146
+ octs.join(".")
147
+ }
148
+ end
149
+ end
150
+
151
+ def need_to_set_values_for klass, fields_and_values
152
+ fields_and_values.each do | field, value |
153
+ add_generator_for klass, field, lambda { value }
154
+ end
155
+ end
156
+
157
+ def add_generator_for klass, field, generator
158
+ @generators[symbol_for(klass)] ||= {}
159
+ @generators[symbol_for(klass)][field] = generator
160
+ end
161
+
162
+ def need_to_set_generators_for klass, fields_and_generators
163
+ fields_and_generators.each do | field, generator |
164
+ add_generator_for klass, field, generator
165
+ end
166
+ end
167
+
168
+ def generate_confirmations_for instance, parameters
169
+ field_names = @confirmed_fields[symbol_for(instance)]
170
+ return if field_names.nil?
171
+ field_names.each do | field_name |
172
+ confirmation_field_name = "#{field_name}_confirmation"
173
+ value = generator.value_for(instance.class, field_name)
174
+ instance.send("#{field_name.to_s}=".to_sym, value) unless parameters.has_key?(field_name.to_sym)
175
+ instance.send("#{confirmation_field_name.to_s}=".to_sym, value) unless parameters.has_key?(confirmation_field_name.to_sym)
176
+ end
177
+ end
178
+
179
+ def generate_values_for instance, parameters
180
+ fields_and_generators = @generators[symbol_for(instance)]
181
+ return if fields_and_generators.nil?
182
+ fields_and_generators.each do | field_name, proc |
183
+ unless parameters.has_key?(field_name.to_sym)
184
+ value = proc.call
185
+ instance.send("#{field_name.to_sym}=".to_sym, value)
186
+ end
187
+ end
188
+ end
189
+
190
+ def register_for_clean_up klass
191
+ @classes_for_clean_up << klass
192
+ end
193
+ end
194
+
195
+ end
196
+
197
+ # Short-cut method for Object::Factory#create_a
198
+ # Also aliased as an, for class names that start with a vowel.
199
+ # instance = a Thingy
200
+ # another_instance = an OtherThingy
201
+ def a klass, parameters = {}
202
+ Object.factory.create_a klass, parameters
203
+ end
204
+
205
+ alias an a
206
+
207
+ # Short-cut method for Object::Factory#create_and_save_a
208
+ def a_saved klass, parameters = {}
209
+ Object.factory.create_and_save_a klass, parameters
210
+ end
211
+
212
+ # Short-cut method for Object::Factory#when_creating_a
213
+ def when_creating_a klass, options = {}
214
+ Object.factory.when_creating_a klass, options
215
+ end
216
+
217
+ alias when_creating_an when_creating_a
218
+
219
+ # Short-cut method for Object::Factory#next_number
220
+ def a_number
221
+ Object.factory.next_number
222
+ end
223
+
224
+ THE_OBJECT_FACTORY_INSTANCE = Object::Factory.new
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{object-factory}
5
+ s.version = "0.2.3"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Brightbox Systems Ltd"]
9
+ s.date = %q{2009-11-10}
10
+ s.description = %q{A simple object factory to help you build valid objects in your tests}
11
+ s.email = %q{hello@brightbox.co.uk}
12
+ s.extra_rdoc_files = ["CHANGELOG", "README.rdoc", "lib/object_factory.rb", "tasks/rspec.rake"]
13
+ s.files = ["CHANGELOG", "Manifest", "README.rdoc", "Rakefile", "github.rb", "init.rb", "lib/object_factory.rb", "object-factory.gemspec", "spec/object_spec.rb", "spec/spec.opts", "tasks/rspec.rake"]
14
+ s.homepage = %q{http://github.com/brightbox/object-factory}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Object-factory", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{object-factory}
18
+ s.rubygems_version = %q{1.3.5}
19
+ s.summary = %q{A simple object factory to help you build valid objects in your tests}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
+ s.add_runtime_dependency(%q<brightbox-rujitsu>, [">= 0"])
27
+ else
28
+ s.add_dependency(%q<brightbox-rujitsu>, [">= 0"])
29
+ end
30
+ else
31
+ s.add_dependency(%q<brightbox-rujitsu>, [">= 0"])
32
+ end
33
+ end
@@ -0,0 +1,337 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/object_factory.rb')
2
+
3
+ class TestClass
4
+ attr_accessor :field, :another_field, :password, :password_confirmation, :other, :other_confirmation
5
+ def initialize parameters = nil
6
+ self.field = parameters[:field]
7
+ self.another_field = parameters[:another_field]
8
+ self.password = parameters[:password]
9
+ self.password_confirmation = parameters[:password_confirmation]
10
+ end
11
+ end
12
+
13
+ class AnotherTestClass
14
+
15
+ end
16
+
17
+ describe Object, "with RSpec/Rails extensions" do
18
+ describe "accessing the factory" do
19
+ it "should return an object factory" do
20
+ Object.factory.class.should == Object::Factory
21
+ end
22
+
23
+ it "should use a single instance" do
24
+ @first_factory = Object.factory
25
+ @second_factory = Object.factory
26
+
27
+ @first_factory.should == @second_factory
28
+ end
29
+ end
30
+ end
31
+
32
+ describe Object::Factory::ValueGenerator do
33
+ it "should generate a unique string value for a given class and field" do
34
+ @generator = Object::Factory::ValueGenerator.new
35
+
36
+ @value = @generator.value_for TestClass, :field
37
+ @value.should match(/TestClass\-field\-(\d+)/)
38
+ end
39
+
40
+ it "should generate a unique integer value" do
41
+ @generator = Object::Factory::ValueGenerator.new
42
+
43
+ @first_value = @generator.unique_integer
44
+ @second_value = @generator.unique_integer
45
+
46
+ @first_value.should_not == @second_value
47
+ end
48
+
49
+ end
50
+
51
+ describe Object::Factory, "creating simple instances" do
52
+
53
+ before :each do
54
+ Object.factory.reset
55
+ end
56
+
57
+ it "should create an instance of the given class with no provided parameters" do
58
+ @test_instance = mock('Test Instance')
59
+ TestClass.should_receive(:new).with({}).and_return(@test_instance)
60
+
61
+ @created_instance = Object.factory.create_a(TestClass)
62
+ @created_instance.should == @test_instance
63
+ end
64
+
65
+ it "should create an instance of the given class with the given parameters" do
66
+ @test_instance = mock('Test Instance')
67
+ TestClass.should_receive(:new).with({:some => :values}).and_return(@test_instance)
68
+
69
+ @created_instance = Object.factory.create_a(TestClass, :some => :values)
70
+ @created_instance.should == @test_instance
71
+ end
72
+
73
+ it "should allow 'a' as a short-cut to creating objects" do
74
+ @test_instance = mock('Test Instance')
75
+ TestClass.should_receive(:new).with({}).and_return(@test_instance)
76
+
77
+ @created_instance = a TestClass
78
+ @created_instance.should == @test_instance
79
+ end
80
+
81
+ it "should allow 'an' as a short-cut to creating objects" do
82
+ @test_instance = mock('Test Instance')
83
+ AnotherTestClass.should_receive(:new).with({}).and_return(@test_instance)
84
+
85
+ @created_instance = an AnotherTestClass
86
+ @created_instance.should == @test_instance
87
+ end
88
+
89
+ it "should auto-save the created object" do
90
+ @test_instance = mock('Test Instance')
91
+ TestClass.should_receive(:new).with({:some => :values}).and_return(@test_instance)
92
+ @test_instance.should_receive(:save).and_return(true)
93
+
94
+ @created_instance = Object.factory.create_and_save_a(TestClass, :some => :values)
95
+ end
96
+
97
+ it "should raise an exception if the auto-saved object cannot be saved" do
98
+ @test_instance = mock('Test Instance')
99
+ TestClass.should_receive(:new).with({:some => :values}).and_return(@test_instance)
100
+ @test_instance.should_receive(:errors).and_return(['errors'])
101
+ @test_instance.should_receive(:save).and_return(false)
102
+
103
+ lambda {
104
+ Object.factory.create_and_save_a(TestClass, :some => :values)
105
+ }.should raise_error(Object::Factory::CannotSaveError)
106
+ end
107
+
108
+ it "should allow 'a_saved' as a short-cut to creating and saving an object" do
109
+ @test_instance = mock('Test Instance')
110
+ TestClass.should_receive(:new).with({:some => :values}).and_return(@test_instance)
111
+ @test_instance.should_receive(:save).and_return(true)
112
+
113
+ @created_instance = a_saved(TestClass, :some => :values)
114
+ end
115
+ end
116
+
117
+ describe Object::Factory, "configuring a class" do
118
+ it "should allow 'when_creating_a' as a short-cut to configuring a class" do
119
+ Object.factory.should_receive(:when_creating_a)
120
+
121
+ when_creating_a TestClass, :auto_generate => :employee_code
122
+ end
123
+
124
+
125
+ end
126
+
127
+ describe Object::Factory, "creating instances with generated values" do
128
+
129
+ before :each do
130
+ Object.factory.reset
131
+ end
132
+
133
+ it "should auto-generate a unique value for a configured field" do
134
+ Object.factory.generator.should_receive(:value_for).with(TestClass, :field).and_return("TestClass-field-1")
135
+
136
+ Object.factory.when_creating_a TestClass, :auto_generate => :field
137
+ @instance = Object.factory.create_a TestClass
138
+ @instance.field.should == 'TestClass-field-1'
139
+ end
140
+
141
+ it "should auto-generate unique values for multiple configured fields" do
142
+ Object.factory.generator.should_receive(:value_for).with(TestClass, :field).and_return("TestClass-field-1")
143
+ Object.factory.generator.should_receive(:value_for).with(TestClass, :another_field).and_return("TestClass-another_field-1")
144
+
145
+ Object.factory.when_creating_a TestClass, :auto_generate => [:field, :another_field]
146
+
147
+ @instance = Object.factory.create_a TestClass
148
+ @instance.field.should match(/TestClass-field-(\d+)/)
149
+ @instance.another_field.should match(/TestClass-another_field-(\d+)/)
150
+ end
151
+
152
+ it "should allow you to override generated values" do
153
+ Object.factory.when_creating_a TestClass, :auto_generate => :field
154
+
155
+ @instance = Object.factory.create_a TestClass, :field => 'My Override Value'
156
+ @instance.field.should == 'My Override Value'
157
+ end
158
+
159
+ it "should allow you to override generated values with nils" do
160
+ Object.factory.when_creating_a TestClass, :auto_generate => :field
161
+
162
+ @instance = Object.factory.create_a TestClass, :field => nil
163
+ @instance.field.should be_nil
164
+ end
165
+ end
166
+
167
+ describe Object::Factory, "creating instances with confirmed values" do
168
+
169
+ before :each do
170
+ Object.factory.reset
171
+ end
172
+
173
+ it "should auto-generate a unique value for a configured field and its confirmation field" do
174
+ Object.factory.generator.should_receive(:value_for).with(TestClass, :password).and_return("TestClass-password-1")
175
+
176
+ Object.factory.when_creating_a TestClass, :auto_confirm => :password
177
+
178
+ @instance = Object.factory.create_a TestClass
179
+ @instance.password.should == 'TestClass-password-1'
180
+ @instance.password_confirmation.should == @instance.password
181
+ end
182
+
183
+ it "should auto-generate unique values for multiple configured fields and confirmation fields" do
184
+ Object.factory.generator.should_receive(:value_for).with(TestClass, :password).and_return("TestClass-password-1")
185
+ Object.factory.generator.should_receive(:value_for).with(TestClass, :other).and_return("TestClass-other-1")
186
+
187
+ Object.factory.when_creating_a TestClass, :auto_confirm => [:password, :other]
188
+
189
+ @instance = Object.factory.create_a TestClass
190
+ @instance.password.should match(/TestClass-password-(\d+)/)
191
+ @instance.password_confirmation.should == @instance.password
192
+ @instance.other.should match(/TestClass-other-(\d+)/)
193
+ @instance.other_confirmation.should == @instance.other
194
+ end
195
+
196
+ it "should allow you to override confirmed original values" do
197
+ Object.factory.when_creating_a TestClass, :auto_confirm => :password
198
+
199
+ @instance = Object.factory.create_a TestClass, :password => 'My Override Value'
200
+ @instance.password.should == 'My Override Value'
201
+ @instance.password_confirmation.should_not == @instance.password
202
+ end
203
+
204
+ it "should allow you to override confirmed confirmation fields" do
205
+ Object.factory.when_creating_a TestClass, :auto_confirm => :password
206
+
207
+ @instance = Object.factory.create_a TestClass, :password_confirmation => 'My Override Value'
208
+ @instance.password_confirmation.should == 'My Override Value'
209
+ @instance.password_confirmation.should_not == @instance.password
210
+ end
211
+
212
+ it "should allow you to override confirmed values with nils" do
213
+ Object.factory.when_creating_a TestClass, :auto_confirm => :password
214
+
215
+ @instance = Object.factory.create_a TestClass, :password => nil
216
+ @instance.password.should be_nil
217
+ @instance.password_confirmation.should_not == @instance.password
218
+ end
219
+
220
+ it "should allow you to override confirmed confirmation fields with nils" do
221
+ Object.factory.when_creating_a TestClass, :auto_confirm => :password
222
+
223
+ @instance = Object.factory.create_a TestClass, :password_confirmation => nil
224
+ @instance.password_confirmation.should be_nil
225
+ @instance.password_confirmation.should_not == @instance.password
226
+ end
227
+ end
228
+
229
+ describe Object::Factory, "setting static values" do
230
+ before :each do
231
+ Object.factory.reset
232
+ end
233
+
234
+ it "should set a static value for a configured field" do
235
+ Object.factory.when_creating_a TestClass, :set => { :field => 'hello' }
236
+ @instance = Object.factory.create_a TestClass
237
+ @instance.field.should == 'hello'
238
+ end
239
+
240
+ it "should set static values for multiple configured fields" do
241
+ Object.factory.when_creating_a TestClass, :set => { :field => 'hello', :another_field => 'world' }
242
+
243
+ @instance = Object.factory.create_a TestClass
244
+ @instance.field.should == 'hello'
245
+ @instance.another_field.should == 'world'
246
+ end
247
+ end
248
+
249
+ describe Object::Factory, "generating email addresses" do
250
+ before :each do
251
+ Object.factory.reset
252
+ end
253
+
254
+ it "should generate a random email address for a configured field" do
255
+ Object.factory.when_creating_a TestClass, :generate_email_address => :field
256
+
257
+ @instance = Object.factory.create_a TestClass
258
+ @instance.field.should match(/(.*)@(.*)\.com/)
259
+ end
260
+
261
+ it "should generate random email addresses for multiple configured fields" do
262
+ Object.factory.when_creating_a TestClass, :generate_email_address => [:field, :another_field]
263
+
264
+ @instance = Object.factory.create_a TestClass
265
+ @instance.field.should match(/(.*)@(.*)\.com/)
266
+ @instance.another_field.should match(/(.*)@(.*)\.com/)
267
+ end
268
+ end
269
+
270
+ describe Object::Factory, "generating ip addresses" do
271
+ before :each do
272
+ Object.factory.reset
273
+ end
274
+
275
+ it "should generate a random ip address for a configured field" do
276
+ Object.factory.when_creating_a TestClass, :generate_ip_address => :field
277
+
278
+ @instance = Object.factory.create_a TestClass
279
+ @instance.field.should match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)
280
+ end
281
+
282
+ it "should generate a random ip address for multiple configured fields" do
283
+ Object.factory.when_creating_a TestClass, :generate_ip_address => [:field, :another_field]
284
+
285
+ @instance = Object.factory.create_a TestClass
286
+ @instance.field.should match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)
287
+ @instance.another_field.should match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)
288
+ end
289
+ end
290
+
291
+ describe Object::Factory, "using lambdas to generate values" do
292
+ before :each do
293
+ Object.factory.reset
294
+ end
295
+
296
+ it "should set a lambda-generator for configured fields" do
297
+ Object.factory.when_creating_a TestClass, :generate => { :field => lambda { "poop" }, :another_field => lambda { Date.today.to_s } }
298
+
299
+ @instance = Object.factory.create_a TestClass
300
+ @instance.field.should == 'poop'
301
+ @instance.another_field.should == Date.today.to_s
302
+ end
303
+ end
304
+
305
+ describe Object::Factory, "generating sequential numbers" do
306
+ before :each do
307
+ Object.factory.reset
308
+ end
309
+
310
+ it "should generate a sequential number" do
311
+ first = Object.factory.next_number
312
+ second = Object.factory.next_number
313
+
314
+ second.should == first + 1
315
+ end
316
+
317
+ it "should use the shortcut to generate a sequential number" do
318
+ Object.factory.should_receive(:next_number).and_return(1)
319
+ number = a_number
320
+ end
321
+ end
322
+
323
+ describe Object::Factory, "cleaning up ActiveRecord models" do
324
+ before :each do
325
+ Object.factory.reset
326
+ end
327
+
328
+ it "should delete all instances for registered classes" do
329
+ Object.factory.when_creating_a TestClass, :auto_confirm => :password, :clean_up => true
330
+ Object.factory.when_creating_an AnotherTestClass, :clean_up => true
331
+
332
+ TestClass.should_receive(:delete_all).and_return(0)
333
+ AnotherTestClass.should_receive(:delete_all).and_return(0)
334
+
335
+ Object.factory.clean_up
336
+ end
337
+ end
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format progress
3
+ --loadby mtime
4
+ --reverse
@@ -0,0 +1,24 @@
1
+ # Borrowed from http://github.com/rsim/ruby-plsql/tree/master/tasks/rspec.rake
2
+ # Github++
3
+ begin
4
+ require "spec"
5
+ rescue LoadError
6
+ require "rubygems"
7
+ require "spec"
8
+ end
9
+
10
+ begin
11
+ require "spec/rake/spectask"
12
+ rescue LoadError
13
+ puts <<-EOS
14
+ To use rspec for testing you must install rspec gem:
15
+ [sudo] gem install rspec
16
+ EOS
17
+ exit(0)
18
+ end
19
+
20
+ desc "Run the specs under spec/*"
21
+ Spec::Rake::SpecTask.new do |t|
22
+ t.spec_opts = ["--options", "spec/spec.opts"]
23
+ t.spec_files = FileList["spec/*_spec.rb"]
24
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: object-factory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.3
5
+ platform: ruby
6
+ authors:
7
+ - Brightbox Systems Ltd
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-10 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: brightbox-rujitsu
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: A simple object factory to help you build valid objects in your tests
26
+ email: hello@brightbox.co.uk
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - CHANGELOG
33
+ - README.rdoc
34
+ - lib/object_factory.rb
35
+ - tasks/rspec.rake
36
+ files:
37
+ - CHANGELOG
38
+ - Manifest
39
+ - README.rdoc
40
+ - Rakefile
41
+ - github.rb
42
+ - init.rb
43
+ - lib/object_factory.rb
44
+ - object-factory.gemspec
45
+ - spec/object_spec.rb
46
+ - spec/spec.opts
47
+ - tasks/rspec.rake
48
+ has_rdoc: true
49
+ homepage: http://github.com/brightbox/object-factory
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --line-numbers
55
+ - --inline-source
56
+ - --title
57
+ - Object-factory
58
+ - --main
59
+ - README.rdoc
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "1.2"
73
+ version:
74
+ requirements: []
75
+
76
+ rubyforge_project: object-factory
77
+ rubygems_version: 1.3.5
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: A simple object factory to help you build valid objects in your tests
81
+ test_files: []
82
+