active_record_block_matchers 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +4 -0
- data/README.md +15 -1
- data/Rakefile +12 -0
- data/bin/setup +1 -1
- data/lib/active_record_block_matchers/create_a_matcher.rb +14 -3
- 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: 0d4300a69d179e6d1179829142e265a79dfca842
|
4
|
+
data.tar.gz: bd6f73e84dc9d9530d6f7771f478e413b68ba91f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 764d8f289679718c85409795111582ed667c7c6e0844cace7a402829f4dace7e63b7e94b12b70e0761e4bb80b99acae71ce6ae96e57348a3bf27b021c365064c
|
7
|
+
data.tar.gz: 700dbeb4e9890cb99da19301725c2fbd723edaa5e8aabf127e7157429dc5862314339e3543ad00f896656cfece516b3a838cfb55086d898239fde578dd9b780f
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
# Build Status
|
2
|
+
[![Build Status](https://travis-ci.org/nwallace/active_record_block_matchers.svg)](https://travis-ci.org/nwallace/active_record_block_matchers)
|
3
|
+
|
1
4
|
# ActiveRecordBlockMatchers
|
2
5
|
|
3
6
|
Custom RSpec matchers for ActiveRecord record creation
|
@@ -52,6 +55,14 @@ expect { User.create!(username: "BOB") }
|
|
52
55
|
.with_attributes(username: "bob")
|
53
56
|
```
|
54
57
|
|
58
|
+
You can even use RSpec's [composable matchers][1]:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
expect { User.create!(username: "bob") }
|
62
|
+
.to create_a_new(User)
|
63
|
+
.with_attributes(username: a_string_starting_with("b"))
|
64
|
+
```
|
65
|
+
|
55
66
|
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
67
|
|
57
68
|
```ruby
|
@@ -71,7 +82,7 @@ This matcher relies on a `created_at` column existing on the given model class.
|
|
71
82
|
|
72
83
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
73
84
|
|
74
|
-
To install this gem onto your local machine, run `bundle exec rake install`.
|
85
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
75
86
|
|
76
87
|
## Contributing
|
77
88
|
|
@@ -80,3 +91,6 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
80
91
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
81
92
|
4. Push to the branch (`git push origin my-new-feature`)
|
82
93
|
5. Create a new Pull Request
|
94
|
+
|
95
|
+
|
96
|
+
[1]: https://www.relishapp.com/rspec/rspec-expectations/v/3-3/docs/composing-matchers
|
data/Rakefile
CHANGED
@@ -1,4 +1,16 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
2
|
require "standalone_migrations"
|
3
|
+
require 'rspec/core/rake_task'
|
3
4
|
|
4
5
|
StandaloneMigrations::Tasks.load_tasks
|
6
|
+
|
7
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
8
|
+
t.pattern = Dir.glob('spec/**/*_spec.rb')
|
9
|
+
t.rspec_opts = '--format documentation'
|
10
|
+
end
|
11
|
+
|
12
|
+
task :travis do
|
13
|
+
Rake::Task['db:setup'].invoke
|
14
|
+
Rake::Task[:spec].invoke
|
15
|
+
end
|
16
|
+
|
data/bin/setup
CHANGED
@@ -27,7 +27,7 @@ RSpec::Matchers.define :create_a_new do |klass|
|
|
27
27
|
@attribute_mismatches = []
|
28
28
|
|
29
29
|
@attributes && @attributes.each do |field, value|
|
30
|
-
unless record.public_send(field)
|
30
|
+
unless values_match?(value, record.public_send(field))
|
31
31
|
@attribute_mismatches << [field, value, record.public_send(field)]
|
32
32
|
end
|
33
33
|
end
|
@@ -48,7 +48,8 @@ RSpec::Matchers.define :create_a_new do |klass|
|
|
48
48
|
"the block should have created 1 #{klass}, but created #{@created_records.count}"
|
49
49
|
elsif @attribute_mismatches.any?
|
50
50
|
@attribute_mismatches.map do |field, expected, actual|
|
51
|
-
|
51
|
+
expected_description = is_composable_matcher?(expected) ? expected.description : expected.inspect
|
52
|
+
"Expected #{field.inspect} to be #{expected_description}, but was #{actual.inspect}"
|
52
53
|
end.join("\n")
|
53
54
|
else
|
54
55
|
@which_failure.message
|
@@ -57,7 +58,7 @@ RSpec::Matchers.define :create_a_new do |klass|
|
|
57
58
|
|
58
59
|
failure_message_when_negated do
|
59
60
|
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
|
+
"the block should not have created a #{klass} with attributes #{format_attributes_hash(@attributes).inspect}, but did"
|
61
62
|
elsif @created_records.count == 1 && @which_block && !@which_failure
|
62
63
|
"the newly created #{klass} should have failed an expectation in the given block, but didn't"
|
63
64
|
elsif @created_records.count == 1
|
@@ -66,6 +67,16 @@ RSpec::Matchers.define :create_a_new do |klass|
|
|
66
67
|
"the block created a #{klass} that matched all given criteria"
|
67
68
|
end
|
68
69
|
end
|
70
|
+
|
71
|
+
def is_composable_matcher?(value)
|
72
|
+
value.respond_to?(:failure_message_when_negated)
|
73
|
+
end
|
74
|
+
|
75
|
+
def format_attributes_hash(attributes)
|
76
|
+
attributes.each_with_object({}) do |(field,value), memo|
|
77
|
+
memo[field] = is_composable_matcher?(value) ? value.description : value
|
78
|
+
end
|
79
|
+
end
|
69
80
|
end
|
70
81
|
|
71
82
|
RSpec::Matchers.alias_matcher :create_a, :create_a_new
|
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.3
|
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-11-19 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.3
|
171
171
|
signing_key:
|
172
172
|
specification_version: 4
|
173
173
|
summary: Additional RSpec custom matchers for ActiveRecord
|