rspec-json_expectations 1.0.0 → 1.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d3da7374ac61ca45bda7f28a31cd3343e11576e
4
- data.tar.gz: 0ff730addd56dc7fc87021b5c358c8b399645c8b
3
+ metadata.gz: 3da6a45ac2d2224520ac476bc2129bd36f7c1e22
4
+ data.tar.gz: ac809ece983d1a0473d770bc7d9c7f5cf20d5275
5
5
  SHA512:
6
- metadata.gz: 9a7aad7424c140606260fbc15bb5e67e61e99e724c2cc0eeb87b8fe6d9f52abf332b4b3ffd7f6d17862d92fd059ed2406583b2e7ab7e9b6faa79aeb002f4c7c7
7
- data.tar.gz: 22c5a1adf525eec9cf4d6e77aeaede263a34db007b47d4212cabb9607e8f06a56caf723cd7631659b88d1dce2a16a1297918702aaa26b72c4468efa91f6d9dea
6
+ metadata.gz: f57addf1c6dda782c53e361b480d4bec2bb93b56e92e385256c76d5c356f5ecfb0551639e197b72a34fb630fc5a4754efaf1defcd30421fbe76c03edd8115bb2
7
+ data.tar.gz: c2555e105a6e9c01d80b5acf776017a86e409b8d79e28ec925efdae20bf708735fb67b4055582d92ffaba10b34313d9547d532eb0ea5db93f313cc840459eed9
data/README.md CHANGED
@@ -94,6 +94,8 @@ rspec ./spec/user_api_spec.rb:17 # User API has basic info about user
94
94
  rspec ./spec/user_api_spec.rb:25 # User API has some additional info about user
95
95
  ```
96
96
 
97
+ For other features look into documentation: https://www.relishapp.com/waterlink/rspec-json-expectations/docs/json-expectations
98
+
97
99
  ## Contributing
98
100
 
99
101
  1. Fork it ( https://github.com/waterlink/rspec-json_expectations/fork )
@@ -0,0 +1,115 @@
1
+ Feature: include_json matcher with hash
2
+
3
+ As a developer I want to be able to test not only JSON responses
4
+ But I want to test my hashes to be correct and see nice error output on failures
5
+
6
+ Background:
7
+ Given a file "spec/spec_helper.rb" with:
8
+ """ruby
9
+ require "rspec/json_expectations"
10
+ """
11
+ And a local "SIMPLE_HASH" with:
12
+ """ruby
13
+ {
14
+ id: 25,
15
+ email: "john.smith@example.com",
16
+ name: "John"
17
+ }
18
+ """
19
+ And a local "BIG_HASH" with:
20
+ """ruby
21
+ {
22
+ id: 25,
23
+ email: "john.smith@example.com",
24
+ password_hash: "super_md5_hash_that_is_unbreakable",
25
+ name: "John",
26
+ profile_id: 39,
27
+ role: "admin"
28
+ }
29
+ """
30
+
31
+ Scenario: Expecting json string to include simple json
32
+ Given a file "spec/simple_example_spec.rb" with:
33
+ """ruby
34
+ require "spec_helper"
35
+
36
+ RSpec.describe "A json response" do
37
+ subject { %{SIMPLE_HASH} }
38
+
39
+ it "has basic info about user" do
40
+ expect(subject).to include_json(
41
+ id: 25,
42
+ email: "john.smith@example.com",
43
+ name: "John"
44
+ )
45
+ end
46
+ end
47
+ """
48
+ When I run "rspec spec/simple_example_spec.rb"
49
+ Then I see:
50
+ """
51
+ 1 example, 0 failures
52
+ """
53
+
54
+ Scenario: Expecting wrong json string to include simple json
55
+ Given a file "spec/simple_with_fail_spec.rb" with:
56
+ """ruby
57
+ require "spec_helper"
58
+
59
+ RSpec.describe "A json response" do
60
+ subject { %{SIMPLE_HASH} }
61
+
62
+ it "has basic info about user" do
63
+ expect(subject).to include_json(
64
+ id: 37,
65
+ email: "john.smith@example.com",
66
+ name: "Smith"
67
+ )
68
+ end
69
+ end
70
+ """
71
+ When I run "rspec spec/simple_with_fail_spec.rb"
72
+ Then I see:
73
+ """
74
+ 1 example, 1 failure
75
+ """
76
+ And I see:
77
+ """
78
+ expected: 37
79
+ got: 25
80
+ """
81
+ And I see:
82
+ """
83
+ expected: "Smith"
84
+ got: "John"
85
+ """
86
+ And I see:
87
+ """ruby
88
+ # ./spec/simple_with_fail_spec.rb
89
+ """
90
+
91
+ Scenario: Expecting json response with excessive fields to include 'smaller' json
92
+ Given a file "spec/excessive_fields_spec.rb" with:
93
+ """ruby
94
+ require "spec_helper"
95
+
96
+ RSpec.describe "A json response" do
97
+ subject { %{BIG_HASH} }
98
+
99
+ it "has basic info about user" do
100
+ expect(subject).to include_json(
101
+ id: 25,
102
+ name: "John",
103
+ profile_id: 39,
104
+ role: "admin"
105
+ )
106
+ end
107
+ end
108
+ """
109
+ When I run "rspec spec/excessive_fields_spec.rb"
110
+ Then I see:
111
+ """
112
+ 1 example, 0 failures
113
+ """
114
+
115
+
@@ -84,7 +84,7 @@ module RSpec
84
84
 
85
85
  def has_key?(actual, key)
86
86
  if actual.is_a?(Hash)
87
- actual.has_key?(key.to_s)
87
+ actual.has_key?(key) || actual.has_key?(key.to_s)
88
88
  elsif actual.is_a?(Array)
89
89
  actual.count > key
90
90
  else
@@ -94,7 +94,7 @@ module RSpec
94
94
 
95
95
  def fetch(actual, key, default=nil)
96
96
  if actual.is_a?(Hash)
97
- actual[key.to_s]
97
+ actual.has_key?(key) ? actual[key] : actual[key.to_s]
98
98
  elsif actual.is_a?(Array)
99
99
  actual[key]
100
100
  else
@@ -41,10 +41,13 @@ RSpec::Matchers.define :include_json do |expected|
41
41
  "Expected value must be a json for include_json matcher"
42
42
  end
43
43
 
44
+ representation = actual
45
+ representation = JSON.parse(actual) if String === actual
46
+
44
47
  RSpec::JsonExpectations::JsonTraverser.traverse(
45
48
  @include_json_errors = { _negate: negate },
46
49
  expected,
47
- JSON.parse(actual),
50
+ representation,
48
51
  negate
49
52
  )
50
53
  end
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module JsonExpectations
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-json_expectations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey Fedorov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-05 00:00:00.000000000 Z
11
+ date: 2014-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -61,6 +61,7 @@ files:
61
61
  - features/rspec/json_expectations/negation_matching.feature
62
62
  - features/rspec/json_expectations/nested_json_support.feature
63
63
  - features/rspec/json_expectations/regular_expressions_support.feature
64
+ - features/rspec/json_expectations/simple_hash_match.feature
64
65
  - features/step_definitions/steps.rb
65
66
  - features/support/env.rb
66
67
  - gemfiles/rspec-2
@@ -103,6 +104,7 @@ test_files:
103
104
  - features/rspec/json_expectations/negation_matching.feature
104
105
  - features/rspec/json_expectations/nested_json_support.feature
105
106
  - features/rspec/json_expectations/regular_expressions_support.feature
107
+ - features/rspec/json_expectations/simple_hash_match.feature
106
108
  - features/step_definitions/steps.rb
107
109
  - features/support/env.rb
108
110
  has_rdoc: