brightbox-object-factory 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +15 -0
- data/README.rdoc +47 -0
- data/Rakefile +17 -0
- data/init.rb +1 -0
- data/lib/object_factory.rb +199 -0
- data/object-factory.gemspec +34 -0
- data/spec/object_spec.rb +302 -0
- data/spec/spec.opts +4 -0
- data/tasks/rspec.rake +24 -0
- metadata +78 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
= Version 0.1.3 (2008-12-08)
|
2
|
+
|
3
|
+
Added generate_ip_address
|
4
|
+
|
5
|
+
= Version 0.1.2 (2008-12-08)
|
6
|
+
|
7
|
+
...
|
8
|
+
|
9
|
+
= Version 0.1.1 2008-11-24
|
10
|
+
|
11
|
+
Added when_creating_a and when_creating_an as short-cut methods
|
12
|
+
|
13
|
+
= Version 0.1.0 (2008-11-24)
|
14
|
+
|
15
|
+
Initial Release
|
data/README.rdoc
ADDED
@@ -0,0 +1,47 @@
|
|
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.
|
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
|
+
== Rails
|
32
|
+
|
33
|
+
To use this with rails, stick the following in your +environment.rb+:
|
34
|
+
|
35
|
+
config.gem "brightbox-object-factory", :lib => "object_factory", :source => "http://gems.github.com"
|
36
|
+
|
37
|
+
== Released under the MIT Licence
|
38
|
+
|
39
|
+
Copyright (c) 2008 Brightbox Systems Ltd
|
40
|
+
|
41
|
+
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:
|
42
|
+
|
43
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
44
|
+
|
45
|
+
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.
|
46
|
+
|
47
|
+
See http://www.brightbox.co.uk/ for contact details.
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('object-factory', '0.1.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
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# placeholder to make this a rails plugin
|
@@ -0,0 +1,199 @@
|
|
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
|
+
end
|
29
|
+
|
30
|
+
# Create a new instance of the given class with the given parameters and apply the auto-generated fields, according to the configured rules
|
31
|
+
def create_a klass, parameters = {}
|
32
|
+
instance = klass.new parameters
|
33
|
+
|
34
|
+
generate_confirmations_for instance, parameters
|
35
|
+
generate_values_for instance, parameters
|
36
|
+
|
37
|
+
return instance
|
38
|
+
end
|
39
|
+
|
40
|
+
# Create a new instance of the given class with the given parameters, auto-generate the field values and then call save!
|
41
|
+
def create_and_save_a klass, parameters = {}
|
42
|
+
instance = create_a klass, parameters
|
43
|
+
raise CannotSaveError, instance.errors.inspect unless instance.save
|
44
|
+
return instance
|
45
|
+
end
|
46
|
+
|
47
|
+
# Set up the required auto-generated fields for the given class.
|
48
|
+
# 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} }
|
49
|
+
# Options are:
|
50
|
+
# * :auto_generate specifies a field name or array of field names that are to have unique string values assigned to them
|
51
|
+
# * :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
|
52
|
+
# * :generate_email_address specifies a field name or array of field names that are set to be randomised email addresses
|
53
|
+
# * :set specifies a Hash of field names and fixed values
|
54
|
+
# * :generate specifies a Hash of field names and lambdas that are used to generate a dynamic value
|
55
|
+
def when_creating_a klass, options = {}
|
56
|
+
need_to_generate_values_for klass, options[:auto_generate] unless options[:auto_generate].nil?
|
57
|
+
need_to_confirm_values_for klass, options[:auto_confirm] unless options[:auto_confirm].nil?
|
58
|
+
need_to_generate_email_addresses_for klass, options[:generate_email_address] unless options[:generate_email_address].nil?
|
59
|
+
need_to_generate_ip_addresses_for klass, options[:generate_ip_address] unless options[:generate_ip_address].nil?
|
60
|
+
need_to_set_values_for klass, options[:set] unless options[:set].nil?
|
61
|
+
need_to_set_generators_for klass, options[:generate] unless options[:generate].nil?
|
62
|
+
end
|
63
|
+
|
64
|
+
alias :when_creating_an :when_creating_a
|
65
|
+
alias :create_and_save_an :create_and_save_a
|
66
|
+
|
67
|
+
# An Object::Factory::ValueGenerator that is used to actually build the unique values used to populate the required fields
|
68
|
+
def generator
|
69
|
+
@generator ||= ValueGenerator.new
|
70
|
+
end
|
71
|
+
|
72
|
+
# A simple class that generates unique values
|
73
|
+
class ValueGenerator
|
74
|
+
def initialize
|
75
|
+
@counter = 0
|
76
|
+
end
|
77
|
+
|
78
|
+
def unique_integer
|
79
|
+
@counter += 1
|
80
|
+
end
|
81
|
+
|
82
|
+
def value_for klass, field
|
83
|
+
"#{klass.name.to_s}-#{field.to_s}-#{unique_integer}"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
#�Error raised when create_and_save_a cannot save the object
|
88
|
+
class CannotSaveError < RuntimeError; end
|
89
|
+
|
90
|
+
# print the rules for a given class
|
91
|
+
def print_configuration_for klass
|
92
|
+
fields_and_generators = @generators[symbol_for(klass)]
|
93
|
+
unless fields_and_generators.nil?
|
94
|
+
fields_and_generators.each do | field_name, generator |
|
95
|
+
puts "#{field_name} uses a lambda"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
private
|
101
|
+
|
102
|
+
def symbol_for object
|
103
|
+
klass = object.is_a?(Class) ? object : object.class
|
104
|
+
return klass.name.to_sym
|
105
|
+
end
|
106
|
+
|
107
|
+
def need_to_generate_values_for klass, fields
|
108
|
+
fields = [fields] unless fields.respond_to?(:each)
|
109
|
+
fields.each do | field |
|
110
|
+
add_generator_for klass, field, lambda { generator.value_for(klass, field) }
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def need_to_confirm_values_for klass, fields
|
115
|
+
fields = [fields] unless fields.respond_to?(:each)
|
116
|
+
@confirmed_fields[symbol_for(klass)] = fields
|
117
|
+
end
|
118
|
+
|
119
|
+
def need_to_generate_email_addresses_for klass, fields
|
120
|
+
fields = [fields] unless fields.respond_to?(:each)
|
121
|
+
fields.each do | field |
|
122
|
+
add_generator_for klass, field, lambda { 6.random_letters + '@' + 10.random_letters + '.com' }
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def need_to_generate_ip_addresses_for klass, fields
|
127
|
+
fields = [fields] unless fields.respond_to?(:each)
|
128
|
+
fields.each do | field |
|
129
|
+
add_generator_for klass, field, lambda {
|
130
|
+
octs = []
|
131
|
+
4.times { octs << 1.random_numbers(:to => 255) }
|
132
|
+
octs.join(".")
|
133
|
+
}
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def need_to_set_values_for klass, fields_and_values
|
138
|
+
fields_and_values.each do | field, value |
|
139
|
+
add_generator_for klass, field, lambda { value }
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def add_generator_for klass, field, generator
|
144
|
+
@generators[symbol_for(klass)] ||= {}
|
145
|
+
@generators[symbol_for(klass)][field] = generator
|
146
|
+
end
|
147
|
+
|
148
|
+
def need_to_set_generators_for klass, fields_and_generators
|
149
|
+
fields_and_generators.each do | field, generator |
|
150
|
+
add_generator_for klass, field, generator
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def generate_confirmations_for instance, parameters
|
155
|
+
field_names = @confirmed_fields[symbol_for(instance)]
|
156
|
+
return if field_names.nil?
|
157
|
+
field_names.each do | field_name |
|
158
|
+
confirmation_field_name = "#{field_name}_confirmation"
|
159
|
+
value = generator.value_for(instance.class, field_name)
|
160
|
+
instance.send("#{field_name.to_s}=".to_sym, value) unless parameters.has_key?(field_name.to_sym)
|
161
|
+
instance.send("#{confirmation_field_name.to_s}=".to_sym, value) unless parameters.has_key?(confirmation_field_name.to_sym)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
def generate_values_for instance, parameters
|
166
|
+
fields_and_generators = @generators[symbol_for(instance)]
|
167
|
+
return if fields_and_generators.nil?
|
168
|
+
fields_and_generators.each do | field_name, proc |
|
169
|
+
value = proc.call
|
170
|
+
instance.send("#{field_name.to_sym}=".to_sym, value) unless parameters.has_key?(field_name.to_sym)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
end
|
176
|
+
|
177
|
+
# Short-cut method for Object::Factory#create_a
|
178
|
+
# Also aliased as an, for class names that start with a vowel.
|
179
|
+
# instance = a Thingy
|
180
|
+
# another_instance = an OtherThingy
|
181
|
+
def a klass, parameters = {}
|
182
|
+
Object.factory.create_a klass, parameters
|
183
|
+
end
|
184
|
+
|
185
|
+
alias an a
|
186
|
+
|
187
|
+
# Short-cut method for Object::Factory#create_and_save_a
|
188
|
+
def a_saved klass, parameters = {}
|
189
|
+
Object.factory.create_and_save_a klass, parameters
|
190
|
+
end
|
191
|
+
|
192
|
+
# Short-cut method for Object::Factory#when_creating_a
|
193
|
+
def when_creating_a klass, options = {}
|
194
|
+
Object.factory.when_creating_a klass, options
|
195
|
+
end
|
196
|
+
|
197
|
+
alias when_creating_an when_creating_a
|
198
|
+
|
199
|
+
THE_OBJECT_FACTORY_INSTANCE = Object::Factory.new
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{object-factory}
|
5
|
+
s.version = "0.1.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{2008-12-08}
|
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", "lib/object_factory.rb", "README.rdoc", "tasks/rspec.rake"]
|
13
|
+
s.files = ["CHANGELOG", "init.rb", "lib/object_factory.rb", "Manifest", "object-factory.gemspec", "Rakefile", "README.rdoc", "spec/object_spec.rb", "spec/spec.opts", "tasks/rspec.rake"]
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.homepage = %q{http://github.com/brightbox/object-factory}
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Object-factory", "--main", "README.rdoc"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{object-factory}
|
19
|
+
s.rubygems_version = %q{1.3.1}
|
20
|
+
s.summary = %q{A simple object factory to help you build valid objects in your tests}
|
21
|
+
|
22
|
+
if s.respond_to? :specification_version then
|
23
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
24
|
+
s.specification_version = 2
|
25
|
+
|
26
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
27
|
+
s.add_runtime_dependency(%q<brightbox-rujitsu>, [">= 0"])
|
28
|
+
else
|
29
|
+
s.add_dependency(%q<brightbox-rujitsu>, [">= 0"])
|
30
|
+
end
|
31
|
+
else
|
32
|
+
s.add_dependency(%q<brightbox-rujitsu>, [">= 0"])
|
33
|
+
end
|
34
|
+
end
|
data/spec/object_spec.rb
ADDED
@@ -0,0 +1,302 @@
|
|
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(:save).and_return(false)
|
101
|
+
|
102
|
+
lambda {
|
103
|
+
Object.factory.create_and_save_a(TestClass, :some => :values)
|
104
|
+
}.should raise_error(Object::Factory::CannotSaveError)
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should allow 'a_saved' as a short-cut to creating and saving an object" do
|
108
|
+
@test_instance = mock('Test Instance')
|
109
|
+
TestClass.should_receive(:new).with({:some => :values}).and_return(@test_instance)
|
110
|
+
@test_instance.should_receive(:save).and_return(true)
|
111
|
+
|
112
|
+
@created_instance = a_saved(TestClass, :some => :values)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
describe Object::Factory, "configuring a class" do
|
117
|
+
it "should allow 'when_creating_a' as a short-cut to configuring a class" do
|
118
|
+
Object.factory.should_receive(:when_creating_a)
|
119
|
+
|
120
|
+
when_creating_a TestClass, :auto_generate => :employee_code
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
end
|
125
|
+
|
126
|
+
describe Object::Factory, "creating instances with generated values" do
|
127
|
+
|
128
|
+
before :each do
|
129
|
+
Object.factory.reset
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should auto-generate a unique value for a configured field" do
|
133
|
+
Object.factory.generator.should_receive(:value_for).with(TestClass, :field).and_return("TestClass-field-1")
|
134
|
+
|
135
|
+
Object.factory.when_creating_a TestClass, :auto_generate => :field
|
136
|
+
@instance = Object.factory.create_a TestClass
|
137
|
+
@instance.field.should == 'TestClass-field-1'
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should auto-generate unique values for multiple configured fields" do
|
141
|
+
Object.factory.generator.should_receive(:value_for).with(TestClass, :field).and_return("TestClass-field-1")
|
142
|
+
Object.factory.generator.should_receive(:value_for).with(TestClass, :another_field).and_return("TestClass-another_field-1")
|
143
|
+
|
144
|
+
Object.factory.when_creating_a TestClass, :auto_generate => [:field, :another_field]
|
145
|
+
|
146
|
+
@instance = Object.factory.create_a TestClass
|
147
|
+
@instance.field.should match(/TestClass-field-(\d+)/)
|
148
|
+
@instance.another_field.should match(/TestClass-another_field-(\d+)/)
|
149
|
+
end
|
150
|
+
|
151
|
+
it "should allow you to override generated values" do
|
152
|
+
Object.factory.when_creating_a TestClass, :auto_generate => :field
|
153
|
+
|
154
|
+
@instance = Object.factory.create_a TestClass, :field => 'My Override Value'
|
155
|
+
@instance.field.should == 'My Override Value'
|
156
|
+
end
|
157
|
+
|
158
|
+
it "should allow you to override generated values with nils" do
|
159
|
+
Object.factory.when_creating_a TestClass, :auto_generate => :field
|
160
|
+
|
161
|
+
@instance = Object.factory.create_a TestClass, :field => nil
|
162
|
+
@instance.field.should be_nil
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
describe Object::Factory, "creating instances with confirmed values" do
|
167
|
+
|
168
|
+
before :each do
|
169
|
+
Object.factory.reset
|
170
|
+
end
|
171
|
+
|
172
|
+
it "should auto-generate a unique value for a configured field and its confirmation field" do
|
173
|
+
Object.factory.generator.should_receive(:value_for).with(TestClass, :password).and_return("TestClass-password-1")
|
174
|
+
|
175
|
+
Object.factory.when_creating_a TestClass, :auto_confirm => :password
|
176
|
+
|
177
|
+
@instance = Object.factory.create_a TestClass
|
178
|
+
@instance.password.should == 'TestClass-password-1'
|
179
|
+
@instance.password_confirmation.should == @instance.password
|
180
|
+
end
|
181
|
+
|
182
|
+
it "should auto-generate unique values for multiple configured fields and confirmation fields" do
|
183
|
+
Object.factory.generator.should_receive(:value_for).with(TestClass, :password).and_return("TestClass-password-1")
|
184
|
+
Object.factory.generator.should_receive(:value_for).with(TestClass, :other).and_return("TestClass-other-1")
|
185
|
+
|
186
|
+
Object.factory.when_creating_a TestClass, :auto_confirm => [:password, :other]
|
187
|
+
|
188
|
+
@instance = Object.factory.create_a TestClass
|
189
|
+
@instance.password.should match(/TestClass-password-(\d+)/)
|
190
|
+
@instance.password_confirmation.should == @instance.password
|
191
|
+
@instance.other.should match(/TestClass-other-(\d+)/)
|
192
|
+
@instance.other_confirmation.should == @instance.other
|
193
|
+
end
|
194
|
+
|
195
|
+
it "should allow you to override confirmed original values" do
|
196
|
+
Object.factory.when_creating_a TestClass, :auto_confirm => :password
|
197
|
+
|
198
|
+
@instance = Object.factory.create_a TestClass, :password => 'My Override Value'
|
199
|
+
@instance.password.should == 'My Override Value'
|
200
|
+
@instance.password_confirmation.should_not == @instance.password
|
201
|
+
end
|
202
|
+
|
203
|
+
it "should allow you to override confirmed confirmation fields" do
|
204
|
+
Object.factory.when_creating_a TestClass, :auto_confirm => :password
|
205
|
+
|
206
|
+
@instance = Object.factory.create_a TestClass, :password_confirmation => 'My Override Value'
|
207
|
+
@instance.password_confirmation.should == 'My Override Value'
|
208
|
+
@instance.password_confirmation.should_not == @instance.password
|
209
|
+
end
|
210
|
+
|
211
|
+
it "should allow you to override confirmed values with nils" do
|
212
|
+
Object.factory.when_creating_a TestClass, :auto_confirm => :password
|
213
|
+
|
214
|
+
@instance = Object.factory.create_a TestClass, :password => nil
|
215
|
+
@instance.password.should be_nil
|
216
|
+
@instance.password_confirmation.should_not == @instance.password
|
217
|
+
end
|
218
|
+
|
219
|
+
it "should allow you to override confirmed confirmation fields with nils" do
|
220
|
+
Object.factory.when_creating_a TestClass, :auto_confirm => :password
|
221
|
+
|
222
|
+
@instance = Object.factory.create_a TestClass, :password_confirmation => nil
|
223
|
+
@instance.password_confirmation.should be_nil
|
224
|
+
@instance.password_confirmation.should_not == @instance.password
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
describe Object::Factory, "setting static values" do
|
229
|
+
before :each do
|
230
|
+
Object.factory.reset
|
231
|
+
end
|
232
|
+
|
233
|
+
it "should set a static value for a configured field" do
|
234
|
+
Object.factory.when_creating_a TestClass, :set => { :field => 'hello' }
|
235
|
+
@instance = Object.factory.create_a TestClass
|
236
|
+
@instance.field.should == 'hello'
|
237
|
+
end
|
238
|
+
|
239
|
+
it "should set static values for multiple configured fields" do
|
240
|
+
Object.factory.when_creating_a TestClass, :set => { :field => 'hello', :another_field => 'world' }
|
241
|
+
|
242
|
+
@instance = Object.factory.create_a TestClass
|
243
|
+
@instance.field.should == 'hello'
|
244
|
+
@instance.another_field.should == 'world'
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
describe Object::Factory, "generating email addresses" do
|
249
|
+
before :each do
|
250
|
+
Object.factory.reset
|
251
|
+
end
|
252
|
+
|
253
|
+
it "should generate a random email address for a configured field" do
|
254
|
+
Object.factory.when_creating_a TestClass, :generate_email_address => :field
|
255
|
+
|
256
|
+
@instance = Object.factory.create_a TestClass
|
257
|
+
@instance.field.should match(/(.*)@(.*)\.com/)
|
258
|
+
end
|
259
|
+
|
260
|
+
it "should generate random email addresses for multiple configured fields" do
|
261
|
+
Object.factory.when_creating_a TestClass, :generate_email_address => [:field, :another_field]
|
262
|
+
|
263
|
+
@instance = Object.factory.create_a TestClass
|
264
|
+
@instance.field.should match(/(.*)@(.*)\.com/)
|
265
|
+
@instance.another_field.should match(/(.*)@(.*)\.com/)
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
describe Object::Factory, "generating ip addresses" do
|
270
|
+
before :each do
|
271
|
+
Object.factory.reset
|
272
|
+
end
|
273
|
+
|
274
|
+
it "should generate a random ip address for a configured field" do
|
275
|
+
Object.factory.when_creating_a TestClass, :generate_ip_address => :field
|
276
|
+
|
277
|
+
@instance = Object.factory.create_a TestClass
|
278
|
+
@instance.field.should match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)
|
279
|
+
end
|
280
|
+
|
281
|
+
it "should generate a random ip address for multiple configured fields" do
|
282
|
+
Object.factory.when_creating_a TestClass, :generate_ip_address => [:field, :another_field]
|
283
|
+
|
284
|
+
@instance = Object.factory.create_a TestClass
|
285
|
+
@instance.field.should match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)
|
286
|
+
@instance.another_field.should match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
describe Object::Factory, "using lambdas to generate values" do
|
291
|
+
before :each do
|
292
|
+
Object.factory.reset
|
293
|
+
end
|
294
|
+
|
295
|
+
it "should set a lambda-generator for configured fields" do
|
296
|
+
Object.factory.when_creating_a TestClass, :generate => { :field => lambda { "poop" }, :another_field => lambda { Date.today.to_s } }
|
297
|
+
|
298
|
+
@instance = Object.factory.create_a TestClass
|
299
|
+
@instance.field.should == 'poop'
|
300
|
+
@instance.another_field.should == Date.today.to_s
|
301
|
+
end
|
302
|
+
end
|
data/spec/spec.opts
ADDED
data/tasks/rspec.rake
ADDED
@@ -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,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: brightbox-object-factory
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brightbox Systems Ltd
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-12-08 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: brightbox-rujitsu
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "0"
|
23
|
+
version:
|
24
|
+
description: A simple object factory to help you build valid objects in your tests
|
25
|
+
email: hello@brightbox.co.uk
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- CHANGELOG
|
32
|
+
- lib/object_factory.rb
|
33
|
+
- README.rdoc
|
34
|
+
- tasks/rspec.rake
|
35
|
+
files:
|
36
|
+
- CHANGELOG
|
37
|
+
- init.rb
|
38
|
+
- lib/object_factory.rb
|
39
|
+
- Manifest
|
40
|
+
- object-factory.gemspec
|
41
|
+
- Rakefile
|
42
|
+
- README.rdoc
|
43
|
+
- spec/object_spec.rb
|
44
|
+
- spec/spec.opts
|
45
|
+
- tasks/rspec.rake
|
46
|
+
has_rdoc: true
|
47
|
+
homepage: http://github.com/brightbox/object-factory
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options:
|
50
|
+
- --line-numbers
|
51
|
+
- --inline-source
|
52
|
+
- --title
|
53
|
+
- Object-factory
|
54
|
+
- --main
|
55
|
+
- README.rdoc
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "1.2"
|
69
|
+
version:
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project: object-factory
|
73
|
+
rubygems_version: 1.2.0
|
74
|
+
signing_key:
|
75
|
+
specification_version: 2
|
76
|
+
summary: A simple object factory to help you build valid objects in your tests
|
77
|
+
test_files: []
|
78
|
+
|