rspec_request_helpers 0.2.3 → 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 +21 -25
- data/README.md +31 -41
- data/lib/rspec_request_helpers/helpers.rb +17 -72
- data/lib/rspec_request_helpers/version.rb +1 -1
- data/rspec_request_helpers.gemspec +3 -5
- metadata +17 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 17e13bcdd36a8111ca76111fce8b9c53c7453d4bcc61817b2840952792afdd21
|
4
|
+
data.tar.gz: d67ab35a255933d2e1ceac515e274474b7cacbc415e3892f66780ccc951c6c94
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 95f028fbf6238c016495c4bef1ab6aed57ba9b16b2f3c8fd7b90b7a40298dc95a26d74c88d732b061a391ca8ef60c8ce5718d2d8bd449ee212f432812fb43be9
|
7
|
+
data.tar.gz: 39749b39469ed69c7cd57478b7cc060fb4b4765bd595a29824720c0ded45669b05f44a164ce2f0d395f3a02806f46e21e429647cba62d871ddbb1bc85300f496
|
data/Gemfile.lock
CHANGED
@@ -1,45 +1,41 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
rspec_request_helpers (0.
|
4
|
+
rspec_request_helpers (0.3.0)
|
5
5
|
actionpack (>= 3.0)
|
6
6
|
activesupport (>= 3.0)
|
7
|
-
|
8
|
-
parser
|
9
|
-
rack (~> 2.0)
|
7
|
+
rack
|
10
8
|
railties (>= 3.0)
|
11
|
-
rspec (
|
9
|
+
rspec (>= 2.4)
|
12
10
|
|
13
11
|
GEM
|
14
12
|
remote: https://rubygems.org/
|
15
13
|
specs:
|
16
|
-
actionpack (
|
17
|
-
actionview (=
|
18
|
-
activesupport (=
|
14
|
+
actionpack (6.0.0)
|
15
|
+
actionview (= 6.0.0)
|
16
|
+
activesupport (= 6.0.0)
|
19
17
|
rack (~> 2.0)
|
20
18
|
rack-test (>= 0.6.3)
|
21
19
|
rails-dom-testing (~> 2.0)
|
22
|
-
rails-html-sanitizer (~> 1.0, >= 1.0
|
23
|
-
actionview (
|
24
|
-
activesupport (=
|
20
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
21
|
+
actionview (6.0.0)
|
22
|
+
activesupport (= 6.0.0)
|
25
23
|
builder (~> 3.1)
|
26
24
|
erubi (~> 1.4)
|
27
25
|
rails-dom-testing (~> 2.0)
|
28
|
-
rails-html-sanitizer (~> 1.
|
29
|
-
activesupport (
|
26
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
27
|
+
activesupport (6.0.0)
|
30
28
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
31
29
|
i18n (>= 0.7, < 2)
|
32
30
|
minitest (~> 5.1)
|
33
31
|
tzinfo (~> 1.1)
|
34
|
-
|
32
|
+
zeitwerk (~> 2.1, >= 2.1.8)
|
35
33
|
builder (3.2.3)
|
36
34
|
coderay (1.1.2)
|
37
35
|
concurrent-ruby (1.1.5)
|
38
36
|
crass (1.0.4)
|
39
37
|
diff-lcs (1.3)
|
40
38
|
erubi (1.8.0)
|
41
|
-
factory_bot (5.0.2)
|
42
|
-
activesupport (>= 4.2.0)
|
43
39
|
i18n (1.6.0)
|
44
40
|
concurrent-ruby (~> 1.0)
|
45
41
|
loofah (2.2.3)
|
@@ -48,27 +44,25 @@ GEM
|
|
48
44
|
method_source (0.9.0)
|
49
45
|
mini_portile2 (2.4.0)
|
50
46
|
minitest (5.11.3)
|
51
|
-
nokogiri (1.10.
|
47
|
+
nokogiri (1.10.4)
|
52
48
|
mini_portile2 (~> 2.4.0)
|
53
|
-
parser (2.6.2.0)
|
54
|
-
ast (~> 2.4.0)
|
55
49
|
pry (0.11.3)
|
56
50
|
coderay (~> 1.1.0)
|
57
51
|
method_source (~> 0.9.0)
|
58
|
-
rack (2.0.
|
52
|
+
rack (2.0.7)
|
59
53
|
rack-test (1.1.0)
|
60
54
|
rack (>= 1.0, < 3)
|
61
55
|
rails-dom-testing (2.0.3)
|
62
56
|
activesupport (>= 4.2.0)
|
63
57
|
nokogiri (>= 1.6)
|
64
|
-
rails-html-sanitizer (1.0
|
58
|
+
rails-html-sanitizer (1.2.0)
|
65
59
|
loofah (~> 2.2, >= 2.2.2)
|
66
|
-
railties (
|
67
|
-
actionpack (=
|
68
|
-
activesupport (=
|
60
|
+
railties (6.0.0)
|
61
|
+
actionpack (= 6.0.0)
|
62
|
+
activesupport (= 6.0.0)
|
69
63
|
method_source
|
70
64
|
rake (>= 0.8.7)
|
71
|
-
thor (>= 0.
|
65
|
+
thor (>= 0.20.3, < 2.0)
|
72
66
|
rake (10.5.0)
|
73
67
|
rspec (3.8.0)
|
74
68
|
rspec-core (~> 3.8.0)
|
@@ -87,6 +81,7 @@ GEM
|
|
87
81
|
thread_safe (0.3.6)
|
88
82
|
tzinfo (1.2.5)
|
89
83
|
thread_safe (~> 0.1)
|
84
|
+
zeitwerk (2.1.10)
|
90
85
|
|
91
86
|
PLATFORMS
|
92
87
|
ruby
|
@@ -95,6 +90,7 @@ DEPENDENCIES
|
|
95
90
|
bundler (~> 1.16)
|
96
91
|
pry
|
97
92
|
rake (~> 10.0)
|
93
|
+
rspec (~> 3.0)
|
98
94
|
rspec_request_helpers!
|
99
95
|
|
100
96
|
BUNDLED WITH
|
data/README.md
CHANGED
@@ -1,55 +1,45 @@
|
|
1
1
|
# Rspec Request Helpers
|
2
2
|
|
3
|
-
This gem provides few
|
4
|
-
|
5
|
-
[![RSpec flow part 1](http://img.youtube.com/vi/YkZYNlUHHOg/1.jpg)](http://www.youtube.com/watch?v=YkZYNlUHHOg) [![RSpec flow part 2](http://img.youtube.com/vi/CB60JdImYC4/2.jpg)](http://www.youtube.com/watch?v=CB60JdImYC4)
|
3
|
+
This gem provides few helpers for request testing
|
6
4
|
|
7
5
|
This rules were influenced by real projects experience so
|
8
|
-
In order to use it
|
9
|
-
|
10
|
-
For every test example you need to define path, headers, params, and expected_response.
|
6
|
+
In order to use it for your `rspec/requests` specs you need to follow convention
|
11
7
|
|
12
|
-
|
13
|
-
|
8
|
+
```ruby
|
9
|
+
RSpec.describe 'Some API' do
|
10
|
+
path { '/api/v1/posts' }
|
11
|
+
headers do
|
12
|
+
{
|
13
|
+
'Content-Type' => 'application/json',
|
14
|
+
'Accept' => 'application/json',
|
15
|
+
'X-App-Token' => user.api_key
|
16
|
+
}
|
17
|
+
end
|
18
|
+
params do
|
19
|
+
{
|
20
|
+
name: name,
|
21
|
+
description: description,
|
22
|
+
author_id: author_id
|
23
|
+
}
|
24
|
+
end
|
25
|
+
response do
|
26
|
+
{ id: 123, name: name, description: description, author: 'Bob Poster' }
|
27
|
+
end
|
28
|
+
|
29
|
+
it `creates post` do
|
30
|
+
# Ex. do_post_and_assert_201_json_parsed
|
31
|
+
end
|
32
|
+
end
|
33
|
+
```
|
14
34
|
|
15
35
|
So what you'll got for that? - Few handy methods for the testing routine
|
16
36
|
|
17
37
|
| Action | Meaning | RSpec Example |
|
18
38
|
|---|---|---|
|
19
|
-
| `do_get`, `do_post`, `do_put`, `do_delete`, `do_patch` | Sends apropriate request to endpoint with valid params and headers | `get(path,
|
39
|
+
| `do_get`, `do_post`, `do_put`, `do_delete`, `do_patch` | Sends apropriate request to endpoint with valid params and headers | `get(path, params, headers)` |
|
20
40
|
| `assert_201_json`, `assert_404_xml`, `assert_422_json` etc | Assert response status code and mime type (Dynamicly generated based on config ) | `expect(response).to have_http_status(status)`<br>`expect(response.content_type).to eq mime_type` |
|
21
|
-
| `
|
22
|
-
|
23
|
-
Version 0.2.0 introduced `database` method which acceps the code block witch allow creation of database records via `FactoryBot` and Rspec `let`, `let!` syntax.
|
24
|
-
|
25
|
-
```ruby
|
26
|
-
#
|
27
|
-
# Usage:
|
28
|
-
# user(:user, name: 'Bob') translates to let(:user) { FactoryBot.create(:user, name: 'Bob') }
|
29
|
-
# user!(:user, name: 'Bob') translates to let!(:user) { FactoryBot.create(:user, name: 'Bob') }
|
30
|
-
# user_list(:user, name: 'Bob', 3) translates to let(:user) { FactoryBot.create_list(:user, 3, name: 'Bob') }
|
31
|
-
# user_list!(:user, name: 'Bob', 3) translates to let!(:user) { FactoryBot.create_list(:user, 3, name: 'Bob') }
|
32
|
-
#
|
33
|
-
database do
|
34
|
-
company2!(:company, name: name, domain_name: domain, location: location_name)
|
35
|
-
company3!(:company, name: name2, domain_name: domain, location: location_name)
|
36
|
-
order1!(:order, :ordered, :with_items, items_count: 1, status: IN_QC, company_id: company3.id, delivered_at: Time.zone.now)
|
37
|
-
order2!(:order, :ordered, :with_items, items_count: 1, status: COMPLETED, company_id: company3.id, delivered_at: Time.zone.now, order_date: (Time.zone.now - 1.day))
|
38
|
-
order3!(:order, :ordered, :with_items, items_count: 1, status: NOT_READY, company_id: company3.id)
|
39
|
-
end
|
40
|
-
```
|
41
|
-
|
42
|
-
Since version 0.2.1 there is an ability to define regular variables which text example depends on in separate block with handy syntax support
|
43
|
-
|
44
|
-
```ruby
|
45
|
-
vars do
|
46
|
-
variant_id { variant.id }
|
47
|
-
issue_id { issue.id }
|
48
|
-
issue_message { 'Packed 5 instead of 3 items' }
|
49
|
-
item_id { item.id }
|
50
|
-
mispick_report { MispickReport.last }
|
51
|
-
end
|
52
|
-
```
|
41
|
+
| `assert_200_json_raw`, `assert_201_json_hash`, `assert_201_json_object` | Same as above plus check against raw, hash or object presentation of response | `expect(response_body).to eq(expected_response)`
|
42
|
+
| `do_post_and_assert_201_json_parsed` | Shorthand for asserting content type, HTTP status code and if parsed body of the response is equal to | `expect(response_hash).to eq(expected_response)` |
|
53
43
|
|
54
44
|
## Installation
|
55
45
|
|
@@ -1,64 +1,6 @@
|
|
1
1
|
module RspecRequestHelpers
|
2
2
|
module Helpers
|
3
3
|
module ClassMethods
|
4
|
-
require 'parser/current'
|
5
|
-
|
6
|
-
class DatabaseDSLTranslator < ::Parser::TreeRewriter
|
7
|
-
def on_send(node)
|
8
|
-
_, method_name, *args = node.children
|
9
|
-
|
10
|
-
factory_attrs = args.map { |e| e.loc.expression.source }.join(', ')
|
11
|
-
|
12
|
-
case method_name
|
13
|
-
when /list!$/
|
14
|
-
replace(node.loc.expression, "let!(:#{method_name.to_s.sub(/_list!$/,'')}) { FactoryBot.create_list(#{factory_attrs}) }")
|
15
|
-
when /list$/
|
16
|
-
replace(node.loc.expression, "let(:#{method_name.to_s.sub(/_list$/,'')}) { FactoryBot.create_list(#{factory_attrs}) }")
|
17
|
-
when /!$/
|
18
|
-
replace(node.loc.expression, "let!(:#{method_name.to_s.sub(/!$/,'')}) { FactoryBot.create(#{factory_attrs}) }")
|
19
|
-
else
|
20
|
-
replace(node.loc.expression, "let(:#{method_name}) { FactoryBot.create(#{factory_attrs}) }")
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
#
|
26
|
-
# Tiny DSL for creating list or single database records
|
27
|
-
# via factory_bot syntax and Rspec :let or :let! methods
|
28
|
-
# Usage:
|
29
|
-
# user(:user, name: 'Bob') is translated to let(:user) { FactoryBot.create(:user, name: 'Bob') }
|
30
|
-
# user!(:user, name: 'Bob') is translated to let!(:user) { FactoryBot.create(:user, name: 'Bob') }
|
31
|
-
# user_list(:user, name: 'Bob', 3) is translated to let(:user) { FactoryBot.create_list(:user, 3, name: 'Bob') }
|
32
|
-
# user_list!(:user, name: 'Bob', 3) is translated to let!(:user) { FactoryBot.create_list(:user, 3, name: 'Bob') }
|
33
|
-
#
|
34
|
-
def database(&block)
|
35
|
-
parser = Parser::CurrentRuby.new
|
36
|
-
rewriter = DatabaseDSLTranslator.new
|
37
|
-
buffer = Parser::Source::Buffer.new('(string)')
|
38
|
-
buffer.source = Parser::CurrentRuby.parse(block.source).children.last.loc.expression.source
|
39
|
-
rspec_factory = rewriter.rewrite(buffer, parser.parse(buffer))
|
40
|
-
self.class_eval(rspec_factory)
|
41
|
-
end
|
42
|
-
|
43
|
-
class VarsDSL
|
44
|
-
def initialize(klass)
|
45
|
-
@klass = klass
|
46
|
-
end
|
47
|
-
|
48
|
-
def method_missing(method_name, *args, &block)
|
49
|
-
case method_name
|
50
|
-
when /!$/
|
51
|
-
@klass.class_eval { let!(:"#{method_name.to_s.sub(/!$/,'')}", &block) }
|
52
|
-
else
|
53
|
-
@klass.class_eval { let(method_name, &block) }
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
def vars(&block)
|
59
|
-
VarsDSL.new(self).instance_eval(&block)
|
60
|
-
end
|
61
|
-
|
62
4
|
def path(&block)
|
63
5
|
let(:path, &block)
|
64
6
|
end
|
@@ -71,7 +13,7 @@ module RspecRequestHelpers
|
|
71
13
|
let(:headers, &block)
|
72
14
|
end
|
73
15
|
|
74
|
-
def
|
16
|
+
def response(&block)
|
75
17
|
let(:expected_response, &block)
|
76
18
|
end
|
77
19
|
end
|
@@ -89,42 +31,45 @@ module RspecRequestHelpers
|
|
89
31
|
end
|
90
32
|
|
91
33
|
def response_body
|
92
|
-
|
34
|
+
response.body
|
93
35
|
end
|
94
36
|
|
95
|
-
def
|
96
|
-
|
37
|
+
def response_hash
|
38
|
+
parse_json(response_body)
|
97
39
|
end
|
98
40
|
|
99
41
|
def response_objects
|
100
|
-
JSON.parse(
|
42
|
+
JSON.parse(response_body, object_class: OpenStruct)
|
101
43
|
end
|
102
44
|
|
103
|
-
def
|
104
|
-
expect(
|
45
|
+
def assert_raw
|
46
|
+
expect(response_body).to eq expected_response
|
105
47
|
end
|
106
48
|
|
107
|
-
def
|
108
|
-
expect(
|
49
|
+
def assert_object
|
50
|
+
expect(response_objects).to have_attributes(expected_response)
|
109
51
|
end
|
110
52
|
|
111
|
-
def
|
112
|
-
expect(
|
53
|
+
def assert_hash
|
54
|
+
expect(response_hash).to eq(expected_response)
|
113
55
|
end
|
114
56
|
|
115
57
|
def self.generate_helpers
|
116
58
|
%i(get post put patch delete).each do |http_verb|
|
117
59
|
define_method :"do_#{http_verb}" do
|
60
|
+
is_not_json_get = (http_verb == :get || headers['Content-Type'] != 'application/json')
|
61
|
+
params_require_normalization = Rails::VERSION::MAJOR < 4 && is_not_json_get
|
62
|
+
normalized_params = params_require_normalization ? params : params.to_json
|
118
63
|
if Rails::VERSION::MAJOR >= 5
|
119
|
-
public_send(http_verb, path, params:
|
64
|
+
public_send(http_verb, path, params: normalized_params, headers: headers)
|
120
65
|
else
|
121
|
-
public_send(http_verb, path,
|
66
|
+
public_send(http_verb, path, normalized_params, headers)
|
122
67
|
end
|
123
68
|
end
|
124
69
|
|
125
70
|
RspecRequestHelpers.configuration.content_types.each do |type, mime_type|
|
126
71
|
RspecRequestHelpers.configuration.symbols_with_status_codes.each do |status, code|
|
127
|
-
%i(
|
72
|
+
%i(raw hash object).each do |resp_assertion|
|
128
73
|
define_method :"assert_#{code}_#{type}" do
|
129
74
|
expect(response).to have_http_status(status)
|
130
75
|
expect(response.content_type).to eq mime_type
|
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
1
|
lib = File.expand_path('../lib', __FILE__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
3
|
require 'rspec_request_helpers/version'
|
@@ -35,13 +34,12 @@ Gem::Specification.new do |spec|
|
|
35
34
|
spec.add_runtime_dependency 'activesupport', version_string
|
36
35
|
spec.add_runtime_dependency 'actionpack', version_string
|
37
36
|
spec.add_runtime_dependency 'railties', version_string
|
38
|
-
spec.add_runtime_dependency '
|
37
|
+
spec.add_runtime_dependency 'rspec', '>= 2.4'
|
39
38
|
|
40
|
-
spec.add_dependency '
|
41
|
-
spec.add_dependency 'rack', '~> 2.0'
|
42
|
-
spec.add_dependency 'parser'
|
39
|
+
spec.add_dependency 'rack'
|
43
40
|
|
44
41
|
spec.add_development_dependency 'bundler', '~> 1.16'
|
42
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
45
43
|
spec.add_development_dependency 'rake', '~> 10.0'
|
46
44
|
spec.add_development_dependency 'pry'
|
47
45
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec_request_helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrii Baran
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-09-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -53,75 +53,61 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '2.4'
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '2.4'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: rack
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
75
|
+
version: '0'
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
82
|
+
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: bundler
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
90
|
-
type: :
|
89
|
+
version: '1.16'
|
90
|
+
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: parser
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
100
|
-
requirements:
|
101
|
-
- - ">="
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
version: '0'
|
104
|
-
type: :runtime
|
105
|
-
prerelease: false
|
106
|
-
version_requirements: !ruby/object:Gem::Requirement
|
107
|
-
requirements:
|
108
|
-
- - ">="
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: '0'
|
96
|
+
version: '1.16'
|
111
97
|
- !ruby/object:Gem::Dependency
|
112
|
-
name:
|
98
|
+
name: rspec
|
113
99
|
requirement: !ruby/object:Gem::Requirement
|
114
100
|
requirements:
|
115
101
|
- - "~>"
|
116
102
|
- !ruby/object:Gem::Version
|
117
|
-
version: '
|
103
|
+
version: '3.0'
|
118
104
|
type: :development
|
119
105
|
prerelease: false
|
120
106
|
version_requirements: !ruby/object:Gem::Requirement
|
121
107
|
requirements:
|
122
108
|
- - "~>"
|
123
109
|
- !ruby/object:Gem::Version
|
124
|
-
version: '
|
110
|
+
version: '3.0'
|
125
111
|
- !ruby/object:Gem::Dependency
|
126
112
|
name: rake
|
127
113
|
requirement: !ruby/object:Gem::Requirement
|