better_assert_difference 0.1.4 → 0.1.5
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.
- checksums.yaml +4 -4
- data/README.md +12 -5
- data/lib/better_assert_difference.rb +12 -3
- data/lib/better_assert_difference/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 79a753ea4125ca79f966f9cd2203a6d7d7c69362
|
4
|
+
data.tar.gz: 07ad13613eb6b1a048134113eeed4561d102d719
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b740b8142a9bfa74c96d00d68ced4d12f4b37d3b6cf64a319fd5cabc3ec7f0bd70c114f8460833f7b1f969d219b612554cd08308ca1effb5f3f4ff21ca06b3db
|
7
|
+
data.tar.gz: 1974a1b7dd5c5c0f0233738c590aca17e971b2372e3c0be6367ef22fca980d193970f3b5fa5c8a034d27c9eae43679f100cde810760a99d845320f7d20af77a1
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
# BetterAssertDifference
|
3
3
|
`assert_difference`, but better.
|
4
4
|
|
5
|
-

|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -41,14 +41,15 @@ You can use `assert_difference` just as usual with string expressions or procs:
|
|
41
41
|
assert_difference('Foo.bar.count', 3) do
|
42
42
|
# block omitted
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
assert_difference(-> { Foo.bar.count }, 2) do
|
46
46
|
# block omitted
|
47
47
|
end
|
48
48
|
```
|
49
49
|
|
50
|
-
#### Implicit call of `count` on ActiveRecord relations
|
51
|
-
Most of the time you'll want to `count`
|
50
|
+
#### Implicit call of `count` on ActiveRecord relations
|
51
|
+
Most of the time you'll want to execute `count` on ActiveRecord::Relation objects, this call is now implicit and you won't have to
|
52
|
+
use a string expression or a proc.
|
52
53
|
```ruby
|
53
54
|
assert_difference(Foo.where(bar: true)) do
|
54
55
|
# block omitted
|
@@ -63,7 +64,13 @@ Specify an expected difference for each expression:
|
|
63
64
|
end
|
64
65
|
```
|
65
66
|
|
66
|
-
|
67
|
+
#### Better error message
|
68
|
+
assert_difference will list all the assertions that have failed, not just the first one.
|
69
|
+
```console
|
70
|
+
Test failure:
|
71
|
+
@items.select(&:nil?).count didn't change by 2
|
72
|
+
@items.count didn't change by 3
|
73
|
+
```
|
67
74
|
## Contributing
|
68
75
|
|
69
76
|
Bug reports and pull requests are welcome on GitHub at https://github.com/julien-meichelbeck/better_assert_difference.
|
@@ -2,20 +2,22 @@ require 'better_assert_difference/version'
|
|
2
2
|
|
3
3
|
module BetterAssertDifference
|
4
4
|
DEFAULT_DIFF = 1
|
5
|
+
ACTIVE_RECORD_ENABLED = !!defined?(ActiveRecord::Base)
|
5
6
|
|
6
7
|
def assert_difference(expression, difference = DEFAULT_DIFF, message = nil, &block)
|
7
8
|
expression_to_diff =
|
8
9
|
if expression.is_a?(Hash)
|
9
10
|
expression
|
10
11
|
else
|
11
|
-
Array(expression).each_with_object({}) { |exp, expression_hash| expression_hash[exp] =
|
12
|
+
Array(expression).each_with_object({}) { |exp, expression_hash| expression_hash[exp] = difference }
|
12
13
|
end
|
14
|
+
|
13
15
|
block_to_diff =
|
14
16
|
expression_to_diff.each_with_object({}) do |(exp, diff), expression_hash|
|
15
17
|
key =
|
16
18
|
if exp.respond_to?(:call)
|
17
19
|
exp
|
18
|
-
elsif
|
20
|
+
elsif ACTIVE_RECORD_ENABLED && active_record?(exp)
|
19
21
|
-> { exp.count }
|
20
22
|
else
|
21
23
|
-> { eval(exp, block.binding) }
|
@@ -29,7 +31,7 @@ module BetterAssertDifference
|
|
29
31
|
errors = []
|
30
32
|
before.zip(after, expression_to_diff) do |before_value, after_value, (exp, diff)|
|
31
33
|
next if before_value + diff == after_value
|
32
|
-
error = "#{exp.inspect} didn't change by #{diff}"
|
34
|
+
error = "#{exp.inspect} didn't change by #{diff} (before: #{before_value}, after: #{after_value})"
|
33
35
|
error = "#{message}.\n#{error}" if message
|
34
36
|
errors << error
|
35
37
|
end
|
@@ -37,4 +39,11 @@ module BetterAssertDifference
|
|
37
39
|
|
38
40
|
retval
|
39
41
|
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def active_record?(expression)
|
46
|
+
expression.respond_to?(:ancestors) &&
|
47
|
+
expression.ancestors.map(&:to_s).include?('ActiveRecord::Base')
|
48
|
+
end
|
40
49
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: better_assert_difference
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Julien Meichelbeck
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|