rspec-hanami 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +27 -0
- data/lib/rspec/hanami/match_type.rb +32 -0
- data/lib/rspec/hanami/matchers.rb +3 -0
- data/lib/rspec/hanami/matchers/include_json.rb +33 -0
- data/lib/rspec/hanami/matchers/match_entity_schema.rb +78 -0
- data/lib/rspec/hanami/version.rb +1 -1
- data/rspec-hanami.gemspec +1 -0
- metadata +19 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 43b921c465a646167dc6a7fd718c546f1be93d73a9c0519394933fd6238bc1d6
|
4
|
+
data.tar.gz: 74c5c0e2953dbf30834a3644109c8fa4bba5ba2eae17e2588b88d9b013fa1974
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a65b86fd97fdb453abe46ab417279911b347f9df014d67bafca54dce29bf7473a39cce4495f4cefe486393696c4784862d345c045b74240bc2dbd080e766a491
|
7
|
+
data.tar.gz: 1787091ed8cbdccffadf9ce860be0af0f86d2bbd248107ef4f16ccbd567b44aa4842645c0756cf93662ba8b39d9def2b3a3d6977980a630fd1a42a012290caa0
|
data/README.md
CHANGED
@@ -114,6 +114,15 @@ expect(response).to match_in_body('Tittle')
|
|
114
114
|
expect(response).to match_in_body(/Tittle\s\d+/)
|
115
115
|
```
|
116
116
|
|
117
|
+
#### `include_json`
|
118
|
+
Passes if `json` string in the body matches with hash arg
|
119
|
+
|
120
|
+
``` ruby
|
121
|
+
response = action.call(params)
|
122
|
+
expect(response).to include_json(name: 'Anton')
|
123
|
+
expect(response).to include_json(user: { name: 'Anton })
|
124
|
+
```
|
125
|
+
|
117
126
|
### Views Specs
|
118
127
|
#### `have_action`
|
119
128
|
Passes if form object has an action
|
@@ -139,6 +148,24 @@ Passes if form object has a field with wanted params
|
|
139
148
|
expect(view.form).to have_field(node: input, type: 'text', id: 'user-first-name')
|
140
149
|
```
|
141
150
|
|
151
|
+
### Entity specs
|
152
|
+
Passes if argument type has a matching with type.
|
153
|
+
You can use `Hanami::Entity::Types` for compare.
|
154
|
+
|
155
|
+
#### `have_attribute`
|
156
|
+
Passes if `:name` has `Types::String` type:
|
157
|
+
|
158
|
+
``` ruby
|
159
|
+
it { expect(User).to have_attribute(:name, Types::String) }
|
160
|
+
```
|
161
|
+
|
162
|
+
#### `have_attributes`
|
163
|
+
Passes if `:name` has `Types::String` type and `:age` has `Types::Int` type:
|
164
|
+
|
165
|
+
``` ruby
|
166
|
+
it { expect(User).to have_attributes(name: Types::String, age: Types::Int) }
|
167
|
+
```
|
168
|
+
|
142
169
|
## Also see
|
143
170
|
|
144
171
|
* <https://github.com/rspec/rspec>
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Hanami
|
3
|
+
class MatchType
|
4
|
+
attr_reader :actual, :expected
|
5
|
+
|
6
|
+
def initialize(actual, expected)
|
7
|
+
@actual = actual
|
8
|
+
@expected = expected
|
9
|
+
end
|
10
|
+
|
11
|
+
def call
|
12
|
+
match_types?
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def match_types?
|
18
|
+
return match_if_sum? if expected.class == Dry::Types::Sum
|
19
|
+
|
20
|
+
actual.is_a?(expected.primitive)
|
21
|
+
end
|
22
|
+
|
23
|
+
def match_if_sum?
|
24
|
+
expected.primitive?(actual)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Syntactic shortcut to reference Hanami::Entity::Types in custom matchers
|
31
|
+
#
|
32
|
+
Types = ::Hanami::Entity::Types
|
@@ -2,11 +2,14 @@ require 'rspec/core/warnings'
|
|
2
2
|
require 'rspec/expectations'
|
3
3
|
require 'rspec/hanami/form_parser'
|
4
4
|
require 'rspec/hanami/match_status'
|
5
|
+
require 'rspec/hanami/match_type'
|
5
6
|
require 'rspec/hanami/matchers/have_http_status'
|
6
7
|
require 'rspec/hanami/matchers/match_in_body'
|
7
8
|
require 'rspec/hanami/matchers/be_status'
|
8
9
|
require 'rspec/hanami/matchers/form_matchers'
|
9
10
|
require 'rspec/hanami/matchers/redirect_to'
|
11
|
+
require 'rspec/hanami/matchers/match_entity_schema'
|
12
|
+
require 'rspec/hanami/matchers/include_json'
|
10
13
|
|
11
14
|
module RSpec
|
12
15
|
module Hanami
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module RSpec
|
4
|
+
module Hanami
|
5
|
+
module Matchers
|
6
|
+
extend ::RSpec::Matchers::DSL
|
7
|
+
|
8
|
+
# @api public
|
9
|
+
# Passes if `json` string in the body matches with hash arg
|
10
|
+
#
|
11
|
+
#
|
12
|
+
# @example Accepts numeric and symbol statuses
|
13
|
+
# response = action.call(params)
|
14
|
+
# expect(response).to include_json(name: 'Anton')
|
15
|
+
# expect(response).to include_json(user: { name: 'Anton })
|
16
|
+
#
|
17
|
+
matcher :include_json do |json_object|
|
18
|
+
attr_reader :actual, :object
|
19
|
+
|
20
|
+
description { "include json #{json_object.to_json}" }
|
21
|
+
|
22
|
+
match do |object|
|
23
|
+
@object = object
|
24
|
+
@actual = object.last.last
|
25
|
+
actual == JSON.generate(json_object)
|
26
|
+
end
|
27
|
+
|
28
|
+
failure_message { |actual| "expect #{object} to include #{json_object} json" }
|
29
|
+
diffable
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Hanami
|
3
|
+
module Matchers
|
4
|
+
extend ::RSpec::Matchers::DSL
|
5
|
+
|
6
|
+
# @api public
|
7
|
+
# Passes if `parameter` has an passed attribute of expected type
|
8
|
+
# Hanami::Entity::Types are allowed
|
9
|
+
#
|
10
|
+
# expect(User).to have_attribute(:name, Type::String)
|
11
|
+
#
|
12
|
+
matcher :have_attribute do |parameter, type|
|
13
|
+
attr_reader :expected_type
|
14
|
+
|
15
|
+
description { "have #{expected_type.name} type" }
|
16
|
+
match do |object|
|
17
|
+
@attributes = object.instance_variable_get(:@attributes)
|
18
|
+
@actual = @attributes[parameter]
|
19
|
+
@expected_type = type
|
20
|
+
|
21
|
+
RSpec::Hanami::MatchType.new(actual, expected_type).call
|
22
|
+
end
|
23
|
+
|
24
|
+
failure_message do |actual|
|
25
|
+
"expected that #{actual.class} should match with #{expected_type.name}"\
|
26
|
+
"\nDiff: #{differ.diff_as_string(actual.class.to_s, expected_type.name)}"
|
27
|
+
end
|
28
|
+
|
29
|
+
failure_message_when_negated do |actual|
|
30
|
+
"expected that #{actual.class} should not match with #{expected_type.name}"
|
31
|
+
end
|
32
|
+
|
33
|
+
def differ
|
34
|
+
RSpec::Support::Differ.new(
|
35
|
+
color: RSpec::Matchers.configuration.color?
|
36
|
+
)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# @api public
|
41
|
+
# Passes if all `parameters` has an passed attribute of expected types.
|
42
|
+
# Hanami::Entity::Types are allowed
|
43
|
+
#
|
44
|
+
# expect(User).to have_attributes(name: Type::String, ...)
|
45
|
+
#
|
46
|
+
matcher :have_attributes do |**parameters|
|
47
|
+
attr_reader :attributes, :actual_types, :expected_types
|
48
|
+
|
49
|
+
description { "have #{expected_types} types" }
|
50
|
+
match do |object|
|
51
|
+
@attributes = object.instance_variable_get(:@attributes)
|
52
|
+
@actual_types = attributes.map { |_attr, type| type.class }.join(', ')
|
53
|
+
@expected_types = parameters.map { |_attr, type| type.name }.join(', ')
|
54
|
+
|
55
|
+
parameters.all? do |attribute, type|
|
56
|
+
actual = attributes[attribute]
|
57
|
+
RSpec::Hanami::MatchType.new(actual, type).call
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
failure_message do |_actual|
|
62
|
+
"expected that #{actual_types} should match with #{expected_types}"\
|
63
|
+
"\nDiff: #{differ.diff_as_string(actual_types, expected_types)}"
|
64
|
+
end
|
65
|
+
|
66
|
+
failure_message_when_negated do |_actual|
|
67
|
+
"expected that #{actual_types} should not match with #{expected_types}"
|
68
|
+
end
|
69
|
+
|
70
|
+
def differ
|
71
|
+
RSpec::Support::Differ.new(
|
72
|
+
color: RSpec::Matchers.configuration.color?
|
73
|
+
)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/lib/rspec/hanami/version.rb
CHANGED
data/rspec-hanami.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-hanami
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anton Davydov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-08-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: hanami-model
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: bundler
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -98,10 +112,13 @@ files:
|
|
98
112
|
- lib/rspec/hanami.rb
|
99
113
|
- lib/rspec/hanami/form_parser.rb
|
100
114
|
- lib/rspec/hanami/match_status.rb
|
115
|
+
- lib/rspec/hanami/match_type.rb
|
101
116
|
- lib/rspec/hanami/matchers.rb
|
102
117
|
- lib/rspec/hanami/matchers/be_status.rb
|
103
118
|
- lib/rspec/hanami/matchers/form_matchers.rb
|
104
119
|
- lib/rspec/hanami/matchers/have_http_status.rb
|
120
|
+
- lib/rspec/hanami/matchers/include_json.rb
|
121
|
+
- lib/rspec/hanami/matchers/match_entity_schema.rb
|
105
122
|
- lib/rspec/hanami/matchers/match_in_body.rb
|
106
123
|
- lib/rspec/hanami/matchers/redirect_to.rb
|
107
124
|
- lib/rspec/hanami/request_helpers.rb
|