fixture_replacement 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/.gitignore +2 -0
  2. data/CHANGELOG.rdoc +720 -0
  3. data/GPL_LICENSE +674 -0
  4. data/MIT_LICENSE +22 -0
  5. data/README.rdoc +154 -0
  6. data/Rakefile +8 -0
  7. data/TODO.rdoc +22 -0
  8. data/VERSION +1 -0
  9. data/contributions.rdoc +48 -0
  10. data/etc/bug_reports/2007_09_28_linoj.txt +51 -0
  11. data/etc/google_analytics +6 -0
  12. data/etc/patches/2007_09_14_default_model_name_with_arguments.diff +26 -0
  13. data/etc/patches/2007_10_14_active_record_specs.diff +396 -0
  14. data/etc/patches/2007_10_14_protected_attributes.diff +26 -0
  15. data/etc/patches/2007_10_14_send_patch.diff +79 -0
  16. data/etc/patches/2007_10_14_spelling_error_in_comments.diff +13 -0
  17. data/etc/patches/2007_10_17_protected_attributes_second_go.diff +212 -0
  18. data/etc/patches/2007_10_18_README.diff +137 -0
  19. data/etc/patches/2007_10_19_readme_tweak.diff +12 -0
  20. data/etc/patches/2007_10_19_silence_migration.diff +12 -0
  21. data/etc/patches/2007_10_25_changed_classify_to_camelize.diff +38 -0
  22. data/etc/patches/2007_11_20_fixture_replacement_generator_should_not_reload_environment_or_boot.patch +13 -0
  23. data/etc/patches/2007_11_20_string_random_refactor_and_extension.patch +104 -0
  24. data/etc/patches/2007_11_20_string_random_refactor_and_extension_v2.patch +108 -0
  25. data/fixture_replacement.gemspec +126 -0
  26. data/lib/fixture_replacement.rb +16 -0
  27. data/lib/fixture_replacement/attribute_builder.rb +129 -0
  28. data/lib/fixture_replacement/class_methods.rb +45 -0
  29. data/lib/fixture_replacement/method_generator.rb +38 -0
  30. data/lib/fixture_replacement/version.rb +13 -0
  31. data/lib/fr.rb +1 -0
  32. data/philosophy_and_bugs.rdoc +128 -0
  33. data/rake_tasks/code_quality.rb +37 -0
  34. data/rake_tasks/docs.rb +59 -0
  35. data/rake_tasks/gem.rb +27 -0
  36. data/rake_tasks/specs.rb +7 -0
  37. data/rake_tasks/tests.rb +7 -0
  38. data/rake_tasks/website.rb +11 -0
  39. data/spec/fixture_replacement/attribute_builder_spec.rb +162 -0
  40. data/spec/fixture_replacement/fixture_replacement_spec.rb +96 -0
  41. data/spec/fixture_replacement/fixtures/classes.rb +78 -0
  42. data/spec/fixture_replacement/fr_spec.rb +7 -0
  43. data/spec/fixture_replacement/integration/attr_protected_attributes_spec.rb +71 -0
  44. data/spec/fixture_replacement/integration/attributes_for_with_invalid_keys_spec.rb +14 -0
  45. data/spec/fixture_replacement/integration/attributes_from_spec_without_block.rb +52 -0
  46. data/spec/fixture_replacement/integration/create_method_spec.rb +61 -0
  47. data/spec/fixture_replacement/integration/cyclic_dependency_spec.rb +32 -0
  48. data/spec/fixture_replacement/integration/default_warnings_spec.rb +29 -0
  49. data/spec/fixture_replacement/integration/extend_spec.rb +37 -0
  50. data/spec/fixture_replacement/integration/has_and_belongs_to_many_spec.rb +64 -0
  51. data/spec/fixture_replacement/integration/new_method_spec.rb +96 -0
  52. data/spec/fixture_replacement/integration/private_methods_spec.rb +43 -0
  53. data/spec/fixture_replacement/integration/public_methods_spec.rb +21 -0
  54. data/spec/fixture_replacement/integration/valid_attributes_spec.rb +41 -0
  55. data/spec/fixture_replacement/integration/validation_spec.rb +54 -0
  56. data/spec/fixture_replacement/regressions/2008_01_21_random_string_with_sti_spec.rb +71 -0
  57. data/spec/fixture_replacement/version_spec.rb +7 -0
  58. data/spec/spec.opts +1 -0
  59. data/spec/spec_helper.rb +16 -0
  60. data/spec/spec_helpers.rb +117 -0
  61. data/test/test_helper.rb +21 -0
  62. data/test/unit/user_test.rb +57 -0
  63. metadata +143 -0
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2008-2009 Scott Taylor (smtlaissezfaire) <scott@railsnewbie.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,154 @@
1
+ = FixtureReplacement (version 3.0.1)
2
+
3
+ == What is FixtureReplacement
4
+
5
+ FixtureReplacement is a Rails[http://rubyonrails.org/] plugin that provides a simple way to
6
+ quickly populate your test database with model objects without having to manage multiple,
7
+ brittle fixture files. You can easily set up complex object graphs (with models which
8
+ reference other models) and add new objects on the fly.
9
+
10
+ Not only can FixtureReplacement make your test data easier to maintain, it can also help
11
+ to make your tests and specs much more readable and intention-revealing by allowing you
12
+ to omit extraneous details and focus only on the attributes that are important for a
13
+ particular behaviour. It works well with both RSpec[http://rspec.rubyforge.org/] and
14
+ Test::Unit[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html].
15
+
16
+ == What's new since 2.0:
17
+
18
+ * default_* is gone in favor of new_*.
19
+ * Cyclic dependencies are no longer an issue. The "overrides hash" (the hash passed to new_* or create_*) can now be processed.
20
+
21
+ See CHANGELOG.rdoc + test suite for further changes.
22
+
23
+ == Installation
24
+
25
+ Install the plugin:
26
+
27
+ git://github.com/smtlaissezfaire/fixturereplacement.git
28
+
29
+ === Using it with RSpec
30
+
31
+ Add the following to your <tt>spec/spec_helper.rb</tt> file, in the configuration section:
32
+
33
+ Spec::Runner.configure do |config|
34
+ config.include FixtureReplacement
35
+ end
36
+
37
+ === Using it with Test::Unit
38
+
39
+ Add the following to your <tt>test/test_helper.rb</tt> file:
40
+
41
+ class Test::Unit::TestCase
42
+ include FixtureReplacement
43
+ end
44
+
45
+ == How to use FixtureReplacement
46
+
47
+ === Defining default attributes
48
+
49
+ At the heart of FixtureReplacement is the <tt>db/example_data.rb</tt> file where you
50
+ define the default attributes for each of your test models. This example shows the default
51
+ attributes for a user:
52
+
53
+ module FixtureReplacement
54
+
55
+ attributes_for :user do |u|
56
+ password = random_string
57
+
58
+ u.value = "a value",
59
+ u.other = "other value",
60
+ u.another = random_string, # random string 10 characters long
61
+ u.one_more = random_string(15), # 15 characters long
62
+ u.password = password,
63
+ u.password_confirmation = password,
64
+ u.associated_object = new_bar # expects attributes_for :bar to be setup
65
+ end
66
+
67
+ end
68
+
69
+ Note that:
70
+
71
+ - A 'random_string' method is provided for attributes whose exact value isn't important; this means you can
72
+ create multiple, unique model instances
73
+ - you can perform arbitrary set-up and execute any Ruby code prior to returning the hash
74
+ (as shown here where a <tt>password</tt> is generated and then used for both the <tt>:password</tt> and
75
+ <tt>:password_confirmation</tt> attributes)
76
+ - a <tt>new_modelname</tt> method is automatically provided that allows you to set up dependent
77
+ model objects (in this case an instance of the <tt>Bar</tt> model)
78
+
79
+ === Available methods
80
+
81
+ Based on the above definition FixtureReplacement makes the following methods available:
82
+
83
+ - <tt>random_string</tt>: generates a random string as shown above
84
+ - <tt>new_user</tt>: equivalent to <tt>User.new</tt> with the attributes for the user.
85
+ - <tt>create_user</tt>: equivalent to <tt>User.create!</tt> with the user's attributes.
86
+ - <tt>valid_user_attributes</tt>: returns a hash of the user's attributes including associations, specified in db/example_data.rb.
87
+
88
+ === Overriding attributes
89
+
90
+ Overrides of specific attributes can be performed as follows:
91
+
92
+ new_user(:thing => "overridden")
93
+ create_user(:thing => "overridden")
94
+
95
+ Overrides can also be used with associations:
96
+
97
+ scott = create_user(:username => "scott")
98
+ post = create_post(:user => scott)
99
+
100
+
101
+ === attr_protected / attr_accessible
102
+
103
+ In the case that the model has an attr_protected field, FixtureReplacement
104
+ will assign the field as if it wasn't protected, which is convenient for testing:
105
+
106
+ class User < ActiveRecord::Base
107
+ attr_protected :admin_status
108
+ end
109
+
110
+ user = create_user(:username => "scott", :admin_status => true)
111
+ user.admin_status # => true
112
+
113
+ === Validate your fixtures (thanks Fixjour)
114
+
115
+ Validate your fixture definitions after including it in the spec helper or test helper:
116
+
117
+ ==== spec_helper.rb:
118
+
119
+ Spec::Runner.configuration do |config|
120
+ config.include FixtureReplacement
121
+ end
122
+
123
+ FixtureReplacement.validate!
124
+
125
+ ==== test_helper.rb
126
+
127
+ class Test::Unit::TestCase
128
+ include FixtureReplacement
129
+ end
130
+
131
+ FixtureReplacement.validate!
132
+
133
+ === Using FixtureReplacement within <tt>script/console</tt>
134
+
135
+ $ ./script/console
136
+ Loading development environment
137
+ >> include FR
138
+ => Object
139
+ >> create_user
140
+ => #<User id: 1, crypted_password: "521faec1c095..." ...>
141
+
142
+ = Philosophy & Disadvantages
143
+
144
+ See philosophy_and_bugs.rdoc
145
+
146
+ = Contributors, Contributions, & BUGS
147
+
148
+ See contributions.rdoc
149
+
150
+ == License
151
+
152
+ This software is dual licensed under the MIT and the GPLv3 Licenses (it's your pick).
153
+
154
+ Copyright 2007-2009 Scott Taylor / smtlaissezfaire[http://github.com/smtlaissezfaire] (scott@railsnewbie.com)
@@ -0,0 +1,8 @@
1
+ require 'rake'
2
+
3
+ Dir.glob("rake_tasks/**/*.rb").each do |file|
4
+ require file
5
+ end
6
+
7
+ desc 'Default: run unit tests (specs + tests).'
8
+ task :default => [:spec, :test]
@@ -0,0 +1,22 @@
1
+
2
+ == Bugs
3
+
4
+ - Allow 'extend FixtureReplacement' as well as 'include FixtureReplacement'
5
+ - Two levels down of inherited fixture bug
6
+
7
+ == Features
8
+
9
+ - Validate fixtures. Steal any other good ideas from FactoryGirl, Fixjour, Machinist, etc.
10
+ - Support SEQUEL & Datamapper. Make it ORM independent
11
+
12
+ == Code Cleanup
13
+
14
+ - Remove ugly method_added hack
15
+
16
+ == ETC
17
+
18
+ - Store old version 2 README
19
+ - Update website info
20
+ - Update with github info
21
+ - Register Lighthouse project
22
+ - Remove TODOs from Rubyforge tracker (http://rubyforge.org/tracker/?group_id=4556)
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 3.0.1
@@ -0,0 +1,48 @@
1
+ = Bugs & Contributing Code
2
+
3
+ If you would like to change how this software works, report a bug, or request a feature, use
4
+ the github issue tracker[http://github.com/smtlaissezfaire/fixturereplacement/issues].
5
+
6
+ == Running the Specs/Tests for FixtureReplacement
7
+
8
+ You will need rspec (version 1.0.8 or later) to run the specs, as well as the sqlite3-ruby gem
9
+ (and sqlite3 installed):
10
+
11
+ % gem install rspec sqlite3-ruby
12
+
13
+ cd into the fixture_replacement plugin directory:
14
+
15
+ % cd vendor/plugins/fixture_replacement
16
+
17
+ Then run with <tt>rake</tt>
18
+
19
+ % rake
20
+
21
+ There are also some tests for test/unit. These mainly serve as regressions, but you are free
22
+ to run them as well.
23
+
24
+ == Test Coverage
25
+
26
+ specdocs[http://replacefixtures.rubyforge.org/specdoc.html], rcov[http://replacefixtures.rubyforge.org/rcov/index.html], flog[http://replacefixtures.rubyforge.org/flog.txt]
27
+
28
+ = Patches, Contributions:
29
+
30
+ Thanks to the following for making this software better:
31
+
32
+ - Greg Bluvshteyn (http://www.m001.net), for bugging me about the naming, and making the wonderful suggestion to use the plugin in the console.
33
+ - Simon Peter Nicholls
34
+ - default_* methods can take a hash (applied in rev. 11)
35
+ - Wincent Colaiuta (http://wincent.com/) - Huge Thanks
36
+ - patch for spelling error in comments (applied in revision 31)
37
+ - patch for specs with sqlite3 (applied in revision 35)
38
+ - patch to ignore attr_protected in mass assignment (applied in revision 57)
39
+ - Most of this README Documentation (applied in revision 62)
40
+ - patch: silencing sqlite3 in memory creation of table output (applied in revision 72)
41
+ - doc patch: typo fix in readme
42
+ - Carl Porth
43
+ - patch: classify should be camelize (applied in revision 74)
44
+ - LinoJ, JW, Matthew Bass, Andy Watts, Dave Spurr
45
+ - bug reports
46
+ - Bryan Helmkamp: Feedback on back associating models.
47
+ - Pat Nakajima: Wonderful ideas from Fixjour. Elimination of default_* methods in favor
48
+ of new_* methods. validation of new instances.
@@ -0,0 +1,51 @@
1
+ Hi Scott,
2
+
3
+ I'm wondering if you have insight into this or if I should go ask somewhere else.
4
+
5
+ I installed 2 plug-in today,
6
+ first fixture_replacement plugin
7
+ and been using it successfully
8
+
9
+ Then I installed validates_email_format_of plugin
10
+ http://code.dunae.ca/validates_email_format_of
11
+
12
+ and added this to my User model
13
+ validates_email_format_of :email
14
+
15
+ then script/server wont start
16
+ When I comment out this line, it's fine
17
+
18
+ Here's the error:
19
+
20
+ $ script/server
21
+ => Booting Mongrel (use 'script/server webrick' to force WEBrick)
22
+ => Rails application starting on http://0.0.0.0:3000
23
+ => Call with -d to detach
24
+ => Ctrl-C to shutdown server
25
+ ** Starting Mongrel listening at 0.0.0.0:3000
26
+ ** Starting Rails with development environment...
27
+ Exiting
28
+ /Users/jonathan/rails/reviewramp/vendor/plugins/fixture_replacement/init.rb:9:in `evaluate': Error in FixtureReplacement Plugin: undefined method `validates_email_format_of' for #<Class:0x241cc68> (RuntimeError)
29
+ from ./script/../config/../vendor/rails/railties/lib/rails/plugin/loader.rb:107:in `evaluate'
30
+ from /Users/jonathan/rails/reviewramp/vendor/rails/activesupport/lib/active_support/core_ext/kernel/reporting.rb:11:in `silence_warnings'
31
+ from ./script/../config/../vendor/rails/railties/lib/rails/plugin/loader.rb:107:in `evaluate'
32
+ from ./script/../config/../vendor/rails/railties/lib/rails/plugin/loader.rb:24:in `load'
33
+ from /Users/jonathan/rails/reviewramp/vendor/rails/railties/lib/initializer.rb:183:in `load_plugins'
34
+ from ./script/../config/../vendor/rails/railties/lib/rails/plugin/locator.rb:16:in `each'
35
+ from ./script/../config/../vendor/rails/railties/lib/rails/plugin/locator.rb:16:in `each'
36
+ from /Users/jonathan/rails/reviewramp/vendor/rails/railties/lib/initializer.rb:182:in `load_plugins'
37
+ ... 31 levels...
38
+ from /Users/jonathan/rails/reviewramp/vendor/rails/activesupport/lib/active_support/dependencies.rb:495:in `require'
39
+ from /Users/jonathan/rails/reviewramp/vendor/rails/railties/lib/commands/server.rb:39
40
+ from script/server:3:in `require'
41
+ from script/server:3
42
+
43
+
44
+ I havent tried it in a different project (without fixture_replacement) but looks like yours is getting the blame... :)
45
+
46
+ (I'm on Rails Edge)
47
+
48
+ -- linoj
49
+
50
+
51
+
@@ -0,0 +1,6 @@
1
+ <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
2
+ </script>
3
+ <script type="text/javascript">
4
+ _uacct = "UA-3080808-2";
5
+ urchinTracker();
6
+ </script>
@@ -0,0 +1,26 @@
1
+ Index: lib/fixture_replacement/fixture_replacement.rb
2
+ ===================================================================
3
+ --- lib/fixture_replacement/fixture_replacement.rb (revision 1151)
4
+ +++ lib/fixture_replacement/fixture_replacement.rb (working copy)
5
+ @@ -40,7 +40,8 @@
6
+ def merge_unevaluated_method(obj, method_for_instantiation, hash={})
7
+ hash.each do |key, value|
8
+ if value.kind_of?(::FixtureReplacement::DelayedEvaluationProc)
9
+ - hash[key] = obj.send("#{method_for_instantiation}_#{value.call}")
10
+ + model_name, args = value.call
11
+ + hash[key] = obj.send("#{method_for_instantiation}_#{model_name}", args)
12
+ end
13
+ end
14
+ end
15
+ @@ -63,9 +64,9 @@
16
+ default_method = "default_#{model_name}".to_sym
17
+
18
+ fixture_module.module_eval do
19
+ - define_method(default_method) do
20
+ + define_method(default_method) do |*args|
21
+ ::FixtureReplacement::DelayedEvaluationProc.new do
22
+ - model_as_string
23
+ + [model_as_string, args[0]]
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,396 @@
1
+ Index: spec/fixture_replacement/fixture_replacement_spec.rb
2
+ ===================================================================
3
+ --- spec/fixture_replacement/fixture_replacement_spec.rb (revision 31)
4
+ +++ spec/fixture_replacement/fixture_replacement_spec.rb (working copy)
5
+ @@ -1,53 +1,52 @@
6
+ -
7
+ require File.dirname(__FILE__) + "/../spec_helper"
8
+ +require 'activesupport'
9
+ +require 'activerecord'
10
+
11
+ -# It would be better if these things were actual mocks/stubs
12
+ -# of ActiveRecord Classes.
13
+ -class ARBase
14
+ - class << self
15
+ - def create!(h={})
16
+ - obj = new(h)
17
+ - obj.save!
18
+ - obj
19
+ - end
20
+ +ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
21
+ +class User < ActiveRecord::Base
22
+ + belongs_to :gender
23
+ + attr_accessor :saved
24
+ + def save!
25
+ + @saved = true
26
+ end
27
+ +end
28
+ +class Gender < ActiveRecord::Base
29
+ + attr_accessor :saved
30
+ + def save!
31
+ + @saved = true
32
+ + end
33
+ +end
34
+ +class Alien < ActiveRecord::Base
35
+ + belongs_to :gender
36
+ +end
37
+
38
+ - def initialize(hash={})
39
+ - @hash = hash
40
+ +ActiveRecord::Schema.define do
41
+ + create_table :users do |t|
42
+ + t.string :key
43
+ + t.string :other_key
44
+ + t.integer :gender_id
45
+ end
46
+ -
47
+ - attr_reader :hash
48
+ -
49
+ - def gender # this would be a has_many call in rails
50
+ - 17
51
+ + create_table :genders do |t|
52
+ + t.string :sex
53
+ end
54
+ -
55
+ - def save!
56
+ - @saved = true
57
+ + create_table :aliens do |t|
58
+ + t.integer :gender_id
59
+ end
60
+ -
61
+ - def saved?
62
+ - @saved || false
63
+ - end
64
+ -
65
+ end
66
+
67
+ -class User < ARBase; end
68
+ -class Gender < ARBase; end
69
+ -class Alien < ARBase; end
70
+ +include FixtureReplacement
71
+
72
+ -
73
+ module FixtureReplacement
74
+ describe Generator, "creation" do
75
+ before :each do
76
+ @generator = Generator.new("user")
77
+ end
78
+
79
+ - it "should take a lowecase-model name as it's paramaters" do
80
+ + it "should take a lowercase-model name as its paramaters" do
81
+ @generator.model_name.should == "user"
82
+ end
83
+
84
+ - it "should be able to take tell the name of model in string" do
85
+ + it "should be able to take tell the name of model in string" do
86
+ @generator.model_name.to_s.should == "user"
87
+ end
88
+
89
+ @@ -84,16 +83,11 @@
90
+ FixtureReplacement.module_eval do
91
+ def user_attributes
92
+ {
93
+ - :key => :val
94
+ + :key => "val"
95
+ }
96
+ end
97
+ end
98
+ @generator = Generator.new("user")
99
+ -
100
+ - @class = Class.new do
101
+ - include FixtureReplacement
102
+ - end
103
+ - @instance = @class.new
104
+ end
105
+
106
+ it "should generate the method default_user in the module" do
107
+ @@ -103,45 +97,34 @@
108
+
109
+ it "should return a ::FixtureReplacement::DelayedEvaluationProc" do
110
+ @generator.generate_default_method
111
+ - @instance.default_user.class.should == ::FixtureReplacement::DelayedEvaluationProc
112
+ + default_user.class.should == ::FixtureReplacement::DelayedEvaluationProc
113
+ end
114
+
115
+ it %(should return the special proc, which in turn should return an array
116
+ of the name of the model ('user') if no params were given) do
117
+ @generator.generate_default_method
118
+ - @instance.default_user.call.should == ["user"]
119
+ + default_user.call.should == ["user"]
120
+ end
121
+
122
+ it %(should return the special proc, which in turn should return an array
123
+ of the name of the model ('user') and the params given) do
124
+ @generator.generate_default_method
125
+ - @instance.default_user({:some => :hash}).call.should == ["user", {:some => :hash}]
126
+ + default_user({:key => "hash"}).call.should == ["user", {:key => "hash"}]
127
+ end
128
+ end
129
+
130
+ describe Generator, "generate_create_method for User when user_attributes is defined (and valid)" do
131
+ - before :each do
132
+ - User.class_eval do
133
+ - def save!
134
+ - @saved = true
135
+ - end
136
+ - end
137
+ -
138
+ + before :each do
139
+ FixtureReplacement.module_eval do
140
+ def user_attributes
141
+ {
142
+ - :key => :val
143
+ + :key => "val"
144
+ }
145
+ end
146
+ end
147
+ @generator = Generator.new("user")
148
+ @generator.generate_new_method
149
+ @generator.generate_create_method
150
+ -
151
+ - @class = Class.new do
152
+ - include FixtureReplacement
153
+ - end
154
+ - @instance = @class.new
155
+ end
156
+
157
+ it "should generate the method create_user in the module" do
158
+ @@ -150,38 +133,32 @@
159
+
160
+ it "should generate the method create_user which takes one parameter - a hash" do
161
+ @generator.generate_create_method
162
+ - @instance.create_user({:key => :value})
163
+ + create_user({:key => "value"})
164
+ end
165
+
166
+ it "should return a user" do
167
+ @generator.generate_create_method
168
+ - @instance.create_user.should be_a_kind_of(User)
169
+ + create_user.should be_a_kind_of(User)
170
+ end
171
+
172
+ - it "should return a user which has been saved (with create!)" do
173
+ + it "should return a user which has been saved (with save!)" do
174
+ @generator.generate_create_method
175
+ - @instance.create_user.should be_saved
176
+ + create_user.saved.should == true
177
+ end
178
+
179
+ it "should overwrite the hash parameters given" do
180
+ @generator.generate_create_method
181
+ - @instance.create_user(:key => :value).hash.should == {:key => :value}
182
+ + create_user(:key => "value").key.should == "value"
183
+ end
184
+
185
+ it "should not overwrite the default hash parameters, if none are given" do
186
+ @generator.generate_create_method
187
+ - @instance.create_user.hash.should == {:key => :val}
188
+ + create_user.key.should == "val"
189
+ end
190
+ end
191
+
192
+ describe Generator, "generate_create_method for User when user_attributes is defined (and valid)" do
193
+ before :each do
194
+ - User.class_eval do
195
+ - def save!
196
+ - @saved = true
197
+ - end
198
+ - end
199
+ -
200
+ FixtureReplacement.module_eval do
201
+ def user_attributes
202
+ {
203
+ @@ -203,60 +180,40 @@
204
+ @generator = Generator.new("user")
205
+ @generator.generate_new_method
206
+ @generator.generate_create_method
207
+ -
208
+ - @class = Class.new do
209
+ - include FixtureReplacement
210
+ - end
211
+ - @instance = @class.new
212
+ end
213
+
214
+ it "should save the associated join models which have a default_* method (if it is not overwritten)" do
215
+ - created_user = @instance.create_user
216
+ - created_gender = created_user.hash[:gender]
217
+ - created_gender.hash.should == {:sex => "Male"}
218
+ + created_gender = create_user.gender
219
+ + created_gender.sex.should == "Male"
220
+ end
221
+
222
+ it "should not save the associated join model, but not as the default_* method (in the case that it is overwritten)" do
223
+ - created_user = @instance.create_user(:gender => Gender.create!(:sex => "Female"))
224
+ - created_gender = created_user.hash[:gender]
225
+ - created_gender.hash.should == {:sex => "Female"}
226
+ + created_user = create_user(:gender => Gender.create!(:sex => "Female"))
227
+ + created_gender = created_user.gender
228
+ + created_gender.sex.should == "Female"
229
+ end
230
+
231
+ - it "should call Gender.create! when the default_gender method is evaluated by default_gender" do
232
+ - gender = Gender.new
233
+ - Gender.should_receive(:create!).and_return gender
234
+ - @instance.create_user
235
+ + it "should call Gender.save! when the default_gender method is evaluated by default_gender" do
236
+ + create_user.gender.saved.should == true
237
+ end
238
+
239
+ - it "should not call Gender.create! if the default_gender is overwritten by another value (say, a string)" do
240
+ - Gender.should_not_receive(:create!)
241
+ - @instance.create_user(:gender => "a string")
242
+ + it "should not call Gender.save! if the default_gender is overwritten by another value" do
243
+ + create_user(:gender => Gender.new).gender.saved.should_not == true
244
+ end
245
+ end
246
+
247
+ describe Generator, "generate_create_method for User when user_attributes is defined, but not valid" do
248
+ - before :each do
249
+ - User.class_eval do
250
+ - def save!
251
+ - @saved = true
252
+ - end
253
+ - end
254
+ -
255
+ + before :each do
256
+ FixtureReplacement.module_eval do
257
+ def user_attributes
258
+ {
259
+ - :key => :val
260
+ + :key => "val"
261
+ }
262
+ end
263
+ end
264
+ @generator = Generator.new("user")
265
+ @generator.generate_new_method
266
+ @generator.generate_create_method
267
+ -
268
+ - @class = Class.new do
269
+ - include FixtureReplacement
270
+ - end
271
+ - @instance = @class.new
272
+ end
273
+
274
+ it "should generate the method create_user in the module" do
275
+ @@ -265,22 +222,9 @@
276
+
277
+ it "should generate the method create_user which takes one parameter - a hash" do
278
+ @generator.generate_create_method
279
+ - @instance.create_user({:key => :value})
280
+ + create_user({:key => "value"})
281
+ end
282
+
283
+ - it "should raise an error with a user which has been saved (with create!)" do
284
+ - User.class_eval do
285
+ - def save!
286
+ - raise
287
+ - end
288
+ - end
289
+ -
290
+ - @generator.generate_create_method
291
+ - lambda {
292
+ - @instance.create_user
293
+ - }.should raise_error
294
+ - end
295
+ -
296
+ end
297
+
298
+ describe Generator, "generate_new_method for User when user_attributes is defined" do
299
+ @@ -290,7 +234,7 @@
300
+ FixtureReplacement.module_eval do
301
+ def user_attributes
302
+ {
303
+ - :key => :val
304
+ + :key => "val"
305
+ }
306
+ end
307
+
308
+ @@ -302,12 +246,6 @@
309
+ end
310
+ @generator = Generator.new("user")
311
+ @generator.generate_new_method
312
+ -
313
+ - @class = Class.new do
314
+ - include FixtureReplacement
315
+ - end
316
+ - @instance = @class.new
317
+ -
318
+ end
319
+
320
+ it "should respond to new_user in the module" do
321
+ @@ -316,19 +254,21 @@
322
+
323
+ it "should return a new User object" do
324
+ User.stub!(:new).and_return @user
325
+ - @instance.new_user.should == @user
326
+ + new_user.should == @user
327
+ end
328
+
329
+ it "should return a new User object with the keys given in user_attributes" do
330
+ - @instance.new_user.hash.should == {:key => :val}
331
+ + new_user.key.should == "val"
332
+ end
333
+
334
+ it "should over-write the User's hash with any hash given to new_user" do
335
+ - @instance.new_user(:key => :other_value).hash.should == {:key => :other_value}
336
+ + new_user(:key => "other_value").key.should == "other_value"
337
+ end
338
+
339
+ it "should add any hash key-value pairs which weren't previously given in user_attributes" do
340
+ - @instance.new_user(:other_key => :other_value).hash.should == {:key => :val, :other_key => :other_value}
341
+ + u = new_user(:other_key => "other_value")
342
+ + u.key.should == "val"
343
+ + u.other_key.should == "other_value"
344
+ end
345
+ end
346
+
347
+ @@ -365,39 +305,30 @@
348
+
349
+ @generator = Generator.new("alien")
350
+ @generator.generate_new_method
351
+ -
352
+ - @class = Class.new do
353
+ - include FixtureReplacement
354
+ - end
355
+ - @instance = @class.new
356
+ end
357
+
358
+ it "should evaluate any of the default_* methods before returning (if no over-writing key is given)" do
359
+ - new_user = @instance.new_user
360
+ - new_gender = new_user.hash[:gender]
361
+ - new_gender.hash.should == {:sex => "Male"}
362
+ + new_gender = new_user.gender
363
+ + new_gender.sex.should == "Male"
364
+ end
365
+
366
+ it %(should evaluate any of the default_* methods before returning, with the hash params given to default_* method) do
367
+ - new_alien = @instance.new_alien
368
+ - new_gender = new_alien.hash[:gender]
369
+ - new_gender.hash.should == {:sex => "unknown"}
370
+ + new_gender = new_alien.gender
371
+ + new_gender.sex.should == "unknown"
372
+ end
373
+
374
+ it "should call Gender.save! when the default_gender method is evaluated by default_gender" do
375
+ - Gender.should_receive(:create!)
376
+ - @instance.new_user
377
+ + new_user.gender.saved.should == true
378
+ end
379
+
380
+ - it "should not call Gender.new if the default_gender is overwritten by another value (say, a string)" do
381
+ - Gender.should_not_receive(:create!)
382
+ - @instance.new_user(:gender => "a string")
383
+ + it "should not call Gender.new if the default_gender is overwritten by another value" do
384
+ + new_user(:gender => Gender.new).gender.saved.should_not == true
385
+ end
386
+
387
+ it "should be able to overwrite a default_* method" do
388
+ - new_user = @instance.new_user(:gender => Gender.create!(:sex => "Female"))
389
+ - created_gender = new_user.hash[:gender]
390
+ - created_gender.hash.should == {:sex => "Female"}
391
+ + new_user = new_user(:gender => Gender.create!(:sex => "Female"))
392
+ + created_gender = new_user.gender
393
+ + created_gender.sex.should == "Female"
394
+ end
395
+ end
396
+