public
Fork of thoughtbot/shoulda
Description: Makes tests easy on the fingers and the eyes
Homepage: http://www.thoughtbot.com/projects/shoulda
Clone URL: git://github.com/rmm5t/shoulda.git
Added new should_differ macro; akin to Active Support's assert_difference.
Ryan McGeary (author)
Tue Jul 08 19:05:46 -0700 2008
commit  9294040046ea64d6c93a92fe3c01f453f3d8450a
tree    36e8211eb8ae7ee1ef0b7e0bc8cffeb519779257
parent  e6e2f6b906edc4ecd6d7f8a77046216e308d1cf0
...
15
16
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
19
20
...
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
0
@@ -15,6 +15,33 @@ module ThoughtBot # :nodoc:
0
           end
0
           fixtures *all_fixtures
0
         end
0
+
0
+        # Macro that creates a test asserting the numeric difference between the
0
+        # return value of an expression that is run before and after the current
0
+        # setup block is run.  This is similar to Active Support's
0
+        # <tt>assert_difference</tt> assertion.
0
+        #
0
+        # Example:
0
+        #
0
+        #   context "Creating a post"
0
+        #     setup do 
0
+        #       Post.create
0
+        #     end
0
+        #
0
+        #     should_differ "Post.count", :by => 1
0
+        #   end
0
+        #
0
+        # While not shown in this example, the <tt>:by</tt> option is optional
0
+        # and defaults to <tt>1</tt>.
0
+        def should_differ(expression, options = {})
0
+          difference = options[:by] || 1
0
+          expression_evaluation = lambda { eval(expression) }
0
+          before = lambda { @_should_differ_value = expression_evaluation.bind(self).call }
0
+          should "differ '#{expression}' by #{difference}", :before => before do
0
+            assert_equal @_should_differ_value + difference, expression_evaluation.bind(self).call
0
+          end
0
+        end
0
+        
0
       end
0
       
0
       # Prints a message to stdout, tagged with the name of the calling method.
...
76
77
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
80
...
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
0
@@ -76,5 +76,22 @@ class HelpersTest < Test::Unit::TestCase # :nodoc:
0
         assert_same_elements([@a, "abc"].flatten, [3, 3, "def", "abc"])
0
       end
0
     end
0
+    
0
+    context "after adding another value" do
0
+      setup do
0
+        @a.push("another")
0
+      end
0
+      
0
+      # testing the default :by option of 1
0
+      should_differ "@a.length" 
0
+    end
0
+    
0
+    context "after adding 3 more values" do
0
+      setup do
0
+        @a.push(1, 2, 3)
0
+      end
0
+      
0
+      should_differ "@a.length", :by => 3
0
+    end
0
   end
0
 end

Comments