assert_difference 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -22,6 +22,14 @@ would require a more verbose syntax:
22
22
  end
23
23
  end
24
24
 
25
+ Expectations can also be ranges, for example:
26
+
27
+ assert_difference "Blog.count" => +1, "Post.count" => 2..5 do # Generate some sample posts when creating a blog
28
+ post :create
29
+ end
30
+
31
+ On top of that, error reporting is improved by displaying all the counters that didn't match except only one.
32
+
25
33
  To use it with Test::Unit add this code:
26
34
 
27
35
  class Test::Unit::TestCase
@@ -11,8 +11,8 @@ Gem::Specification.new do |s|
11
11
  s.authors = ["J. Pablo Fernández"]
12
12
  s.email = ["pupeno@pupeno.com"]
13
13
  s.homepage = "http://pupeno.github.com/assert_difference/"
14
- s.summary = "An improved assert_difference"
15
- s.description = "Better assert_difference than Rails by providing a more compact and readable syntax through hashes. For some more information read http://pupeno.com/blog/better-assert-difference."
14
+ s.summary = "Like Rails' assert_difference, but more powerful"
15
+ s.description = "Like Rails' assert_difference, but more compact and readable syntax through hashes, testing ranges and improved error reporting."
16
16
 
17
17
  s.required_rubygems_version = ">= 1.3.6"
18
18
  s.rubyforge_project = "assert_difference"
@@ -2,7 +2,7 @@
2
2
  # Copyright © 2010, 2011, 2012 José Pablo Fernández
3
3
 
4
4
  module AssertDifference
5
- VERSION = "0.4.2" unless defined?(::AssertDifference::VERSION)
5
+ VERSION = "0.5.0" unless defined?(::AssertDifference::VERSION)
6
6
 
7
7
  # Test numeric difference between the return value of an expression as a result of what is evaluated
8
8
  # in the yielded block.
@@ -58,17 +58,23 @@ module AssertDifference
58
58
  # end
59
59
  # assert_equal [@user.email], email.to
60
60
  #
61
+ # the expectations can also be ranges, for example:
62
+ #
63
+ # assert_difference "Article.count" => 1, "sample_coments.count" => 2..4 do
64
+ # post :something
65
+ # end
66
+ #
61
67
  # @param [Array, Hash] expressions array of expressions to evaluate or hash
62
68
  # table of expressions and expected difference.
63
- # @param [Integer] difference expected difference when using an array or single expression.
69
+ # @param [Integer or Range] expected_difference expected difference when using an array or single expression.
64
70
  # @param [String, nil] message error message to display. One would be constructed if nil.
65
71
  # @return whatever the block returned
66
- def assert_difference(expressions, difference = 1, message = nil, &block)
72
+ def assert_difference(expressions, expected_difference = 1, message = nil, &block)
67
73
  b = block.send(:binding)
68
74
  if !expressions.is_a? Hash
69
75
  exps = Array.wrap(expressions)
70
76
  expressions = {}
71
- exps.each { |e| expressions[e] = difference }
77
+ exps.each { |e| expressions[e] = expected_difference }
72
78
  end
73
79
 
74
80
  before = {}
@@ -78,9 +84,13 @@ module AssertDifference
78
84
 
79
85
  error_messages = []
80
86
  expressions.each do |exp, diff|
81
- expected = before[exp] + diff
87
+ expected = if diff.is_a? Range
88
+ (before[exp] + diff.first)..(before[exp] + diff.end)
89
+ else
90
+ before[exp] + diff
91
+ end
82
92
  actual = eval(exp, b)
83
- if expected != actual
93
+ if expected.is_a?(Range) ? !expected.include?(actual) : expected != actual
84
94
  error = "#{exp.inspect} didn't change by #{diff} (expecting #{expected}, but got #{actual})"
85
95
  error = "#{message}.\n#{error}" if message
86
96
  error_messages << error
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assert_difference
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -27,8 +27,8 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
- description: Better assert_difference than Rails by providing a more compact and readable
31
- syntax through hashes. For some more information read http://pupeno.com/blog/better-assert-difference.
30
+ description: Like Rails' assert_difference, but more compact and readable syntax through
31
+ hashes, testing ranges and improved error reporting.
32
32
  email:
33
33
  - pupeno@pupeno.com
34
34
  executables: []
@@ -67,6 +67,6 @@ rubyforge_project: assert_difference
67
67
  rubygems_version: 1.8.24
68
68
  signing_key:
69
69
  specification_version: 3
70
- summary: An improved assert_difference
70
+ summary: Like Rails' assert_difference, but more powerful
71
71
  test_files: []
72
72
  has_rdoc: