brightbox-object-factory 0.1.3 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,3 +1,11 @@
1
+ = Version 0.2.0 (2009-05-20)
2
+
3
+ Added clean_up options
4
+
5
+ = Version 0.1.4 (2009-03-25)
6
+
7
+ Added a_number to allow the generation of unique integers
8
+
1
9
  = Version 0.1.3 (2008-12-08)
2
10
 
3
11
  Added generate_ip_address
data/Manifest ADDED
@@ -0,0 +1,11 @@
1
+ CHANGELOG
2
+ github.rb
3
+ init.rb
4
+ lib/object_factory.rb
5
+ Manifest
6
+ object-factory.gemspec
7
+ Rakefile
8
+ README.rdoc
9
+ spec/object_spec.rb
10
+ spec/spec.opts
11
+ tasks/rspec.rake
data/README.rdoc CHANGED
@@ -10,7 +10,7 @@ If you don't use the a_saved method then it should work with any type of model,
10
10
 
11
11
  == Usage
12
12
 
13
- See the wiki page at http://github.com/brightbox/object-factory/wikis/home for the latest docs.
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
14
 
15
15
  However, it works something like this:
16
16
 
@@ -28,11 +28,33 @@ And your options are:
28
28
  * :set => { :field3 => 'value3', :field4 => 'value4' }: sets the given fields to the supplied static values
29
29
  * :generate => { :field5 => lambda { Date.today } }: sets the given fields to the supplied dynamic values
30
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
+
31
51
  == Rails
32
52
 
33
53
  To use this with rails, stick the following in your +environment.rb+:
34
54
 
35
55
  config.gem "brightbox-object-factory", :lib => "object_factory", :source => "http://gems.github.com"
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.
36
58
 
37
59
  == Released under the MIT Licence
38
60
 
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('object-factory', '0.1.3') do | config |
5
+ Echoe.new('object-factory', '0.2.2') do | config |
6
6
  config.description = 'A simple object factory to help you build valid objects in your tests'
7
7
  config.url = 'http://github.com/brightbox/object-factory'
8
8
  config.author = 'Brightbox Systems Ltd'
data/github.rb ADDED
@@ -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"
@@ -25,6 +25,14 @@ class Object
25
25
  def reset
26
26
  @confirmed_fields = {}
27
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
28
36
  end
29
37
 
30
38
  # Create a new instance of the given class with the given parameters and apply the auto-generated fields, according to the configured rules
@@ -59,6 +67,7 @@ class Object
59
67
  need_to_generate_ip_addresses_for klass, options[:generate_ip_address] unless options[:generate_ip_address].nil?
60
68
  need_to_set_values_for klass, options[:set] unless options[:set].nil?
61
69
  need_to_set_generators_for klass, options[:generate] unless options[:generate].nil?
70
+ register_for_clean_up klass if options[:clean_up]
62
71
  end
63
72
 
64
73
  alias :when_creating_an :when_creating_a
@@ -69,6 +78,11 @@ class Object
69
78
  @generator ||= ValueGenerator.new
70
79
  end
71
80
 
81
+ # Generate a unique Integer
82
+ def next_number
83
+ generator.unique_integer
84
+ end
85
+
72
86
  # A simple class that generates unique values
73
87
  class ValueGenerator
74
88
  def initialize
@@ -84,7 +98,7 @@ class Object
84
98
  end
85
99
  end
86
100
 
87
- #�Error raised when create_and_save_a cannot save the object
101
+ Error raised when create_and_save_a cannot save the object
88
102
  class CannotSaveError < RuntimeError; end
89
103
 
90
104
  # print the rules for a given class
@@ -97,7 +111,7 @@ class Object
97
111
  end
98
112
  end
99
113
 
100
- private
114
+ private
101
115
 
102
116
  def symbol_for object
103
117
  klass = object.is_a?(Class) ? object : object.class
@@ -170,6 +184,10 @@ class Object
170
184
  instance.send("#{field_name.to_sym}=".to_sym, value) unless parameters.has_key?(field_name.to_sym)
171
185
  end
172
186
  end
187
+
188
+ def register_for_clean_up klass
189
+ @classes_for_clean_up << klass
190
+ end
173
191
  end
174
192
 
175
193
  end
@@ -196,4 +214,9 @@ end
196
214
 
197
215
  alias when_creating_an when_creating_a
198
216
 
217
+ # Short-cut method for Object::Factory#next_number
218
+ def a_number
219
+ Object.factory.next_number
220
+ end
221
+
199
222
  THE_OBJECT_FACTORY_INSTANCE = Object::Factory.new
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{object-factory}
5
- s.version = "0.1.3"
5
+ s.version = "0.2.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Brightbox Systems Ltd"]
9
- s.date = %q{2008-12-08}
9
+ s.date = %q{2009-05-21}
10
10
  s.description = %q{A simple object factory to help you build valid objects in your tests}
11
11
  s.email = %q{hello@brightbox.co.uk}
12
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"]
13
+ s.files = ["CHANGELOG", "github.rb", "init.rb", "lib/object_factory.rb", "Manifest", "object-factory.gemspec", "Rakefile", "README.rdoc", "spec/object_spec.rb", "spec/spec.opts", "tasks/rspec.rake"]
14
14
  s.has_rdoc = true
15
15
  s.homepage = %q{http://github.com/brightbox/object-factory}
16
16
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Object-factory", "--main", "README.rdoc"]
data/spec/object_spec.rb CHANGED
@@ -97,6 +97,7 @@ describe Object::Factory, "creating simple instances" do
97
97
  it "should raise an exception if the auto-saved object cannot be saved" do
98
98
  @test_instance = mock('Test Instance')
99
99
  TestClass.should_receive(:new).with({:some => :values}).and_return(@test_instance)
100
+ @test_instance.should_receive(:errors).and_return(['errors'])
100
101
  @test_instance.should_receive(:save).and_return(false)
101
102
 
102
103
  lambda {
@@ -299,4 +300,38 @@ describe Object::Factory, "using lambdas to generate values" do
299
300
  @instance.field.should == 'poop'
300
301
  @instance.another_field.should == Date.today.to_s
301
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
302
337
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brightbox-object-factory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brightbox Systems Ltd
@@ -9,11 +9,12 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-12-08 00:00:00 -08:00
12
+ date: 2009-05-21 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: brightbox-rujitsu
17
+ type: :runtime
17
18
  version_requirement:
18
19
  version_requirements: !ruby/object:Gem::Requirement
19
20
  requirements:
@@ -34,6 +35,7 @@ extra_rdoc_files:
34
35
  - tasks/rspec.rake
35
36
  files:
36
37
  - CHANGELOG
38
+ - github.rb
37
39
  - init.rb
38
40
  - lib/object_factory.rb
39
41
  - Manifest