public
Rubygem
Description: Makes tests easy on the fingers and the eyes
Homepage: http://www.thoughtbot.com/projects/shoulda
Clone URL: git://github.com/thoughtbot/shoulda.git
Reorganized ActiveRecord macros and assertions
rmm5t (author)
Sun Aug 31 14:55:31 -0700 2008
commit  84ea9466f70c45b19802afc384af84652faaac6e
tree    1a2d08e2684a2966aeb856ae75abf01cc12a2d21
parent  526b12317667ececa8b253591133836575116c1d
...
43
44
45
46
 
47
48
49
...
43
44
45
 
46
47
48
49
0
@@ -43,7 +43,7 @@ Produces the following test methods:
0
 
0
 So readable!
0
 
0
-=== ActiveRecord Tests (ThoughtBot::Shoulda::ActiveRecord)
0
+=== ActiveRecord Tests (ThoughtBot::Shoulda::ActiveRecord::Macros)
0
 
0
 Quick macro tests for your ActiveRecord associations and validations:
0
 
...
6
7
8
9
 
10
11
12
13
14
15
16
 
17
18
19
...
37
38
39
40
41
42
43
44
45
46
47
...
6
7
8
 
9
10
11
12
13
14
15
 
16
17
18
19
...
37
38
39
 
40
41
 
 
42
43
44
0
@@ -6,14 +6,14 @@ module Test # :nodoc: all
0
   module Unit
0
     class TestCase
0
       extend Thoughtbot::Shoulda
0
- include Thoughtbot::Shoulda::Assertions
0
+ include ThoughtBot::Shoulda::Assertions
0
     end
0
   end
0
 end
0
 
0
 require 'shoulda/private_helpers'
0
 require 'shoulda/general'
0
-require 'shoulda/active_record_helpers'
0
+require 'shoulda/active_record'
0
 require 'shoulda/controller_tests/controller_tests.rb'
0
 require 'yaml'
0
 
0
@@ -37,11 +37,8 @@ require 'shoulda/color' if shoulda_options[:color]
0
 module Test # :nodoc: all
0
   module Unit
0
     class TestCase
0
-
0
       include ThoughtBot::Shoulda::General
0
       include ThoughtBot::Shoulda::Controller
0
-
0
- extend ThoughtBot::Shoulda::ActiveRecord
0
     end
0
   end
0
 end
...
1
 
2
3
4
...
 
1
2
3
4
0
@@ -1,4 +1,4 @@
0
-module Thoughtbot # :nodoc:
0
+module ThoughtBot # :nodoc:
0
   module Shoulda # :nodoc:
0
     module Assertions
0
       # Asserts that two arrays contain the same elements, the same number of times. Essentially ==, but unordered.
...
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
...
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
...
8
9
10
 
 
 
 
 
 
 
 
11
12
13
...
83
84
85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
87
88
...
114
115
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
118
119
0
@@ -8,14 +8,6 @@ module ThoughtBot # :nodoc:
0
       end
0
 
0
       module ClassMethods
0
- # Loads all fixture files (<tt>test/fixtures/*.yml</tt>)
0
- def load_all_fixtures
0
- all_fixtures = Dir.glob(File.join(Test::Unit::TestCase.fixture_path, "*.yml")).collect do |f|
0
- File.basename(f, '.yml').to_sym
0
- end
0
- fixtures *all_fixtures
0
- end
0
-
0
         # Macro that creates a test asserting a change between the return value
0
         # of an expression that is run before and after the current setup block
0
         # is run. This is similar to Active Support's <tt>assert_difference</tt>
0
@@ -91,21 +83,6 @@ module ThoughtBot # :nodoc:
0
         puts("#{caller.first}: #{msg}")
0
       end
0
 
0
- # Asserts that the given object can be saved
0
- #
0
- # assert_save User.new(params)
0
- def assert_save(obj)
0
- assert obj.save, "Errors: #{pretty_error_messages obj}"
0
- obj.reload
0
- end
0
-
0
- # Asserts that the given object is valid
0
- #
0
- # assert_valid User.new(params)
0
- def assert_valid(obj)
0
- assert obj.valid?, "Errors: #{pretty_error_messages obj}"
0
- end
0
-
0
       # Asserts that an email was delivered. Can take a block that can further
0
       # narrow down the types of emails you're expecting.
0
       #
0
@@ -137,68 +114,6 @@ module ThoughtBot # :nodoc:
0
         ActionMailer::Base.deliveries.each { |m| msg << " '#{m.subject}' sent to #{m.to.to_sentence}\n" }
0
         assert ActionMailer::Base.deliveries.empty?, msg
0
       end
0
-
0
- # Asserts that an Active Record model validates with the passed
0
- # <tt>value</tt> by making sure the <tt>error_message_to_avoid</tt> is not
0
- # contained within the list of errors for that attribute.
0
- #
0
- # assert_good_value(User.new, :email, "user@example.com")
0
- # assert_good_value(User.new, :ssn, "123456789", /length/)
0
- #
0
- # If a class is passed as the first argument, a new object will be
0
- # instantiated before the assertion. If an instance variable exists with
0
- # the same name as the class (underscored), that object will be used
0
- # instead.
0
- #
0
- # assert_good_value(User, :email, "user@example.com")
0
- #
0
- # @product = Product.new(:tangible => false)
0
- # assert_good_value(Product, :price, "0")
0
- def assert_good_value(object_or_klass, attribute, value, error_message_to_avoid = //)
0
- object = get_instance_of(object_or_klass)
0
- object.send("#{attribute}=", value)
0
- object.valid?
0
- assert_does_not_contain(object.errors.on(attribute), error_message_to_avoid, "when set to #{value.inspect}")
0
- end
0
-
0
- # Asserts that an Active Record model invalidates the passed
0
- # <tt>value</tt> by making sure the <tt>error_message_to_expect</tt> is
0
- # contained within the list of errors for that attribute.
0
- #
0
- # assert_bad_value(User.new, :email, "invalid")
0
- # assert_bad_value(User.new, :ssn, "123", /length/)
0
- #
0
- # If a class is passed as the first argument, a new object will be
0
- # instantiated before the assertion. If an instance variable exists with
0
- # the same name as the class (underscored), that object will be used
0
- # instead.
0
- #
0
- # assert_bad_value(User, :email, "invalid")
0
- #
0
- # @product = Product.new(:tangible => true)
0
- # assert_bad_value(Product, :price, "0")
0
- def assert_bad_value(object_or_klass, attribute, value, error_message_to_expect = /invalid/)
0
- object = get_instance_of(object_or_klass)
0
- object.send("#{attribute}=", value)
0
- assert !object.valid?, "#{object.class} allowed #{value.inspect} as a value for #{attribute}"
0
- assert object.errors.on(attribute), "There are no errors on #{attribute} after being set to #{value.inspect}"
0
- assert_contains(object.errors.on(attribute), error_message_to_expect, "when set to #{value.inspect}")
0
- end
0
-
0
- def pretty_error_messages(obj)
0
- obj.errors.map { |a, m| "#{a} #{m} (#{obj.send(a).inspect})" }
0
- end
0
-
0
- private
0
-
0
- def get_instance_of(object_or_klass)
0
- if object_or_klass.is_a?(Class)
0
- klass = object_or_klass
0
- instance_variable_get("@#{klass.to_s.underscore}") || klass.new
0
- else
0
- object_or_klass
0
- end
0
- end
0
     end
0
   end
0
 end
...
1
2
3
4
 
5
6
7
...
1
2
3
 
4
5
6
7
0
@@ -1,7 +1,7 @@
0
 require File.join(File.dirname(__FILE__), '..', 'test_helper')
0
 
0
 class PrivateHelpersTest < Test::Unit::TestCase # :nodoc:
0
- include ThoughtBot::Shoulda::ActiveRecord
0
+ include ThoughtBot::Shoulda::Private
0
   context "get_options!" do
0
     should "remove opts from args" do
0
       args = [:a, :b, {}]

Comments

    No one has commented yet.