rspec-request_snapshot 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +20 -0
- data/lib/rspec/request_snapshot/handlers/base.rb +4 -0
- data/lib/rspec/request_snapshot/handlers/json.rb +19 -27
- data/lib/rspec/request_snapshot/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea875af5bb2032c8034482032dc737d7ec60b55e2f977551adddee0d0bb5f0cf
|
4
|
+
data.tar.gz: 946666e33db659d180bd8762d8cbfc1400f1bcd28c25725a3cc13d639486385e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b78cc8aa841430a3d9134b263bd092e31ddadb9b91fee63683dadf9ed856730ecae3d4844d2bcec0d230c2e2e3559f9617d1940c8435925fc0221bcf7614f05b
|
7
|
+
data.tar.gz: 1b327bc6fec76fda28c437a6093e2d43fb025af9361dde518ee8e11f1a7967da1cd88b889fa71a4d76e0df4e75f93642d7b327e62186ed0c1402a35dde7751f9
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -56,14 +56,34 @@ expect(response.body).to match_snapshot("api/resources_index")
|
|
56
56
|
|
57
57
|
# Using plain text instead of parsing JSON
|
58
58
|
expect(response.body).to match_snapshot("api/resources_index", format: :text)
|
59
|
+
```
|
60
|
+
|
61
|
+
#### Matcher options
|
62
|
+
|
63
|
+
##### dynamic_attributes
|
59
64
|
|
65
|
+
Using `dynamic_attributes` inline allows to ignore attributes for a specific snapshot.
|
66
|
+
This is useful to ignore changing attributes, like `id` or `created_at`.
|
67
|
+
Notice that **all** nodes matching those (nested or not) will be ignored. Usage:
|
68
|
+
|
69
|
+
```ruby
|
60
70
|
# Defining specific test dynamic attributes
|
61
71
|
expect(response.body).to match_snapshot("api/resources_index", dynamic_attributes: %w(confirmed_at relation_id))
|
72
|
+
```
|
73
|
+
|
74
|
+
##### ignore_order
|
75
|
+
|
76
|
+
It is possible to use the `ignore_order` inline option to mark which array nodes are unsorted and that elements position
|
77
|
+
should not be taken into consideration.
|
62
78
|
|
79
|
+
```ruby
|
63
80
|
# Ignoring order for certain arrays (this will ignore the ordering for the countries array inside the json response)
|
64
81
|
expect(response.body).to match_snapshot("api/resources_index", ignore_order: %w(countries))
|
65
82
|
```
|
66
83
|
|
84
|
+
**Note:** If you are using `ignore_order` for arrays of objects/hashes (ie: `[{name: "name", value: "value"}, ...]`),
|
85
|
+
it won't perform well depending on the array and hash size (number of keys)
|
86
|
+
|
67
87
|
## Development
|
68
88
|
|
69
89
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -8,5 +8,9 @@ module Rspec::RequestSnapshot::Handlers
|
|
8
8
|
@dynamic_attributes ||= RSpec.configuration.request_snapshots_dynamic_attributes |
|
9
9
|
Array(@options[:dynamic_attributes])
|
10
10
|
end
|
11
|
+
|
12
|
+
def ignore_order
|
13
|
+
@ignore_order ||= @options[:ignore_order] || []
|
14
|
+
end
|
11
15
|
end
|
12
16
|
end
|
@@ -1,22 +1,10 @@
|
|
1
1
|
class Rspec::RequestSnapshot::Handlers::JSON < Rspec::RequestSnapshot::Handlers::Base
|
2
2
|
def compare(actual, expected)
|
3
|
-
|
4
|
-
|
5
|
-
actual.each do |key, value|
|
6
|
-
if actual[key].is_a?(Hash)
|
7
|
-
equal &= compare_hash(key, actual, expected)
|
8
|
-
elsif actual[key].is_a?(Array)
|
9
|
-
equal &= compare_array(key, actual, expected)
|
10
|
-
else
|
11
|
-
equal &= compare_value(key, actual, expected)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
equal
|
3
|
+
actual == expected
|
16
4
|
end
|
17
5
|
|
18
6
|
def comparable(str)
|
19
|
-
JSON.parse(str)
|
7
|
+
deep_transform_values(JSON.parse(str).dup)
|
20
8
|
end
|
21
9
|
|
22
10
|
def writable(str)
|
@@ -25,21 +13,25 @@ class Rspec::RequestSnapshot::Handlers::JSON < Rspec::RequestSnapshot::Handlers:
|
|
25
13
|
|
26
14
|
private
|
27
15
|
|
28
|
-
def
|
29
|
-
|
30
|
-
|
16
|
+
def deep_transform_values(hash)
|
17
|
+
hash.each do |key, value|
|
18
|
+
if dynamic_attributes.include?(key)
|
19
|
+
hash[key] = "REPLACED"
|
20
|
+
end
|
31
21
|
|
32
|
-
|
33
|
-
|
34
|
-
if @options[:ignore_order] && @options[:ignore_order].include?(key)
|
35
|
-
actual[key].sort == expected[key].sort
|
36
|
-
else
|
37
|
-
actual[key] == expected[key]
|
22
|
+
if hash[key].is_a?(Hash)
|
23
|
+
deep_transform_values(hash[key])
|
38
24
|
end
|
39
|
-
end
|
40
|
-
end
|
41
25
|
|
42
|
-
|
43
|
-
|
26
|
+
if hash[key].is_a?(Array)
|
27
|
+
hash[key].each do |value|
|
28
|
+
deep_transform_values(value) if value.is_a?(Hash)
|
29
|
+
end
|
30
|
+
|
31
|
+
if ignore_order.include?(key)
|
32
|
+
hash[key].first.is_a?(Hash) ? hash[key].sort_by! { |e| e.keys.map { |k| e[k] } } : hash[key].sort!
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
44
36
|
end
|
45
37
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-request_snapshot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bruno Campos
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-01-
|
11
|
+
date: 2019-01-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|