active_record_block_matchers 0.1.1 → 0.1.2
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 +13 -0
- data/lib/active_record_block_matchers/create_a_matcher.rb +36 -14
- data/lib/active_record_block_matchers/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fcffbe7450c275e8f73887b109de06322a94a8f4
|
4
|
+
data.tar.gz: 5446e27ecf51297ba5a652776b957422118c6013
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 091547dfd038261e271f38391d842261e955f5f44f0ac55631bc1ec4d330bcaa203387a97bc8cf257fdfba2a55066922875aacbbc9acc51fbec4d464fa6fa474
|
7
|
+
data.tar.gz: d39f564c0f58925ae8bec51c5a0d6073a3fea5be6fc74a1c40456806a2443a4f4315ba02428e1793164e9b8284ada9562ba9adbe9a9df13cca42d2dc781c2c01
|
data/README.md
CHANGED
@@ -52,6 +52,19 @@ expect { User.create!(username: "BOB") }
|
|
52
52
|
.with_attributes(username: "bob")
|
53
53
|
```
|
54
54
|
|
55
|
+
If you need to make assertions about things other than attribute equality, you can also chain `.which` with a block, and your block will receive the newly created record:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
expect { User.create!(username: "BOB", password: "BlueSteel45") }
|
59
|
+
.to create_a_new(User)
|
60
|
+
.which { |user|
|
61
|
+
expect(user.encrypted_password).to be_present
|
62
|
+
expect(AuthLibrary.authenticate("bob", "BlueSteel45")).to eq user
|
63
|
+
}
|
64
|
+
```
|
65
|
+
|
66
|
+
**Gotcha Warning:** Be careful about your block syntax when chaining `.which` in your tests. If you write the above example with a `do...end`, the example will parse like this: `expect {...}.to(create_a_new(User).which) do |user| ... end`, so your block will not execute, and it may appear that your test is passing, when it is not.
|
67
|
+
|
55
68
|
This matcher relies on a `created_at` column existing on the given model class. The name of this column can be configured via `ActiveRecordBlockMatchers::Config.created_at_column_name = "your_column_name"`
|
56
69
|
|
57
70
|
## Development
|
@@ -1,47 +1,69 @@
|
|
1
1
|
RSpec::Matchers.define :create_a_new do |klass|
|
2
2
|
supports_block_expectations
|
3
3
|
|
4
|
+
description do
|
5
|
+
"create a #{klass}, optionally verifying attributes"
|
6
|
+
end
|
7
|
+
|
4
8
|
chain(:with_attributes) do |attributes|
|
5
9
|
@attributes = attributes
|
6
10
|
end
|
7
11
|
|
12
|
+
chain(:which) do |&block|
|
13
|
+
@which_block = block
|
14
|
+
end
|
15
|
+
|
8
16
|
match do |block|
|
9
|
-
@attributes ||= {}
|
10
17
|
time_before = Time.current
|
18
|
+
|
11
19
|
block.call
|
20
|
+
|
12
21
|
column_name = ActiveRecordBlockMatchers::Config.created_at_column_name
|
13
22
|
@created_records = klass.where("#{column_name} > ?", time_before)
|
14
23
|
return false unless @created_records.count == 1
|
15
24
|
|
16
|
-
@failures = []
|
17
25
|
record = @created_records.first
|
18
|
-
|
26
|
+
|
27
|
+
@attribute_mismatches = []
|
28
|
+
|
29
|
+
@attributes && @attributes.each do |field, value|
|
19
30
|
unless record.public_send(field) == value
|
20
|
-
@
|
31
|
+
@attribute_mismatches << [field, value, record.public_send(field)]
|
21
32
|
end
|
22
33
|
end
|
23
|
-
@failures.empty?
|
24
|
-
end
|
25
34
|
|
26
|
-
|
27
|
-
|
35
|
+
if @attribute_mismatches.none? && @which_block
|
36
|
+
begin
|
37
|
+
@which_block.call(record)
|
38
|
+
rescue RSpec::Expectations::ExpectationNotMetError => e
|
39
|
+
@which_failure = e
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
@attribute_mismatches.empty? && @which_failure.nil?
|
28
44
|
end
|
29
45
|
|
30
46
|
failure_message do
|
31
47
|
if @created_records.count != 1
|
32
48
|
"the block should have created 1 #{klass}, but created #{@created_records.count}"
|
33
|
-
|
34
|
-
@
|
35
|
-
"Expected #{field} to be
|
49
|
+
elsif @attribute_mismatches.any?
|
50
|
+
@attribute_mismatches.map do |field, expected, actual|
|
51
|
+
"Expected #{field.inspect} to be #{expected.inspect}, but was #{actual.inspect}"
|
36
52
|
end.join("\n")
|
53
|
+
else
|
54
|
+
@which_failure.message
|
37
55
|
end
|
38
56
|
end
|
39
57
|
|
40
58
|
failure_message_when_negated do
|
41
|
-
if @created_records.count == 1
|
42
|
-
"the block should not have created a #{klass}
|
59
|
+
if @created_records.count == 1 && @attributes && @attribute_mismatches.none?
|
60
|
+
"the block should not have created a #{klass} with attributes #{@attributes.inspect}, but did"
|
61
|
+
elsif @created_records.count == 1 && @which_block && !@which_failure
|
62
|
+
"the newly created #{klass} should have failed an expectation in the given block, but didn't"
|
63
|
+
elsif @created_records.count == 1
|
64
|
+
"the block should not have created a #{klass}, but created #{@created_records.count}: #{@created_records.inspect}"
|
43
65
|
else
|
44
|
-
"the block created a #{klass} that matched all given
|
66
|
+
"the block created a #{klass} that matched all given criteria"
|
45
67
|
end
|
46
68
|
end
|
47
69
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_block_matchers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Wallace
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-03-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -167,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
167
167
|
version: '0'
|
168
168
|
requirements: []
|
169
169
|
rubyforge_project:
|
170
|
-
rubygems_version: 2.4.
|
170
|
+
rubygems_version: 2.4.5
|
171
171
|
signing_key:
|
172
172
|
specification_version: 4
|
173
173
|
summary: Additional RSpec custom matchers for ActiveRecord
|