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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 99dde53b6b3cdf370ea4d38a79cca61fb7e73b433019da817c65fbd9a6c3b0ca
4
- data.tar.gz: 4b713ccbebe9399f23effd83a2d8dfe9c18b2132b1516b5f9c71c8f3975688c9
3
+ metadata.gz: 17e13bcdd36a8111ca76111fce8b9c53c7453d4bcc61817b2840952792afdd21
4
+ data.tar.gz: d67ab35a255933d2e1ceac515e274474b7cacbc415e3892f66780ccc951c6c94
5
5
  SHA512:
6
- metadata.gz: 86f0a680809c597d6c0136b486f2d73886bf5434fa845b9cb53dea584eb28741cab9e39c9fb3b038a35a79af43f15a80903befa8943025ab2813aa04364cf5be
7
- data.tar.gz: fa75a1d9c6fba201ba9fd5da5c382af659a286bae0c94fb9d384cf89f603b62fec448389f9bb8cd08dbfd5235c9e73e9e254347219260f08694d6232d417bd02
6
+ metadata.gz: 95f028fbf6238c016495c4bef1ab6aed57ba9b16b2f3c8fd7b90b7a40298dc95a26d74c88d732b061a391ca8ef60c8ce5718d2d8bd449ee212f432812fb43be9
7
+ data.tar.gz: 39749b39469ed69c7cd57478b7cc060fb4b4765bd595a29824720c0ded45669b05f44a164ce2f0d395f3a02806f46e21e429647cba62d871ddbb1bc85300f496
@@ -1,45 +1,41 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rspec_request_helpers (0.2.3)
4
+ rspec_request_helpers (0.3.0)
5
5
  actionpack (>= 3.0)
6
6
  activesupport (>= 3.0)
7
- factory_bot
8
- parser
9
- rack (~> 2.0)
7
+ rack
10
8
  railties (>= 3.0)
11
- rspec (~> 3.0)
9
+ rspec (>= 2.4)
12
10
 
13
11
  GEM
14
12
  remote: https://rubygems.org/
15
13
  specs:
16
- actionpack (5.2.2.1)
17
- actionview (= 5.2.2.1)
18
- activesupport (= 5.2.2.1)
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.2)
23
- actionview (5.2.2.1)
24
- activesupport (= 5.2.2.1)
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.0, >= 1.0.3)
29
- activesupport (5.2.2.1)
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
- ast (2.4.0)
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.2)
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.6)
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.4)
58
+ rails-html-sanitizer (1.2.0)
65
59
  loofah (~> 2.2, >= 2.2.2)
66
- railties (5.2.2.1)
67
- actionpack (= 5.2.2.1)
68
- activesupport (= 5.2.2.1)
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.19.0, < 2.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 tools to make testing of API endpoint in your Rails application more effective.
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 in your `rspec/requests` specs you need to follow one rule:
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
- NOTE: version 0.1.1 introduced the helpers which ensure naming conventions. They have identical names and in fact are
13
- wrappers around RSpec `let` functionality.
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, valid_params, valid_headers)` |
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
- | `do_post_and_assert_201_json_response_body` | Shorthand for asserting content type, HTTP status code and if parsed body of the response is equal to | `expect(parsed_body).to eq(expected_response)` |
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 expected_response(&block)
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
- JSON.parse(response.body, symbolize_names: true)
34
+ response.body
93
35
  end
94
36
 
95
- def response_object
96
- OpenStruct.new(responce_body)
37
+ def response_hash
38
+ parse_json(response_body)
97
39
  end
98
40
 
99
41
  def response_objects
100
- JSON.parse(response.body, object_class: OpenStruct)
42
+ JSON.parse(response_body, object_class: OpenStruct)
101
43
  end
102
44
 
103
- def assert_body
104
- expect(response.body).to eq expected_response
45
+ def assert_raw
46
+ expect(response_body).to eq expected_response
105
47
  end
106
48
 
107
- def assert_response_object
108
- expect(response_object).to have_attributes(expected_response)
49
+ def assert_object
50
+ expect(response_objects).to have_attributes(expected_response)
109
51
  end
110
52
 
111
- def assert_response_body
112
- expect(response_body).to eq(expected_response)
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: params, headers: headers)
64
+ public_send(http_verb, path, params: normalized_params, headers: headers)
120
65
  else
121
- public_send(http_verb, path, params, headers)
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(body response_object response_body).each do |resp_assertion|
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,3 +1,3 @@
1
1
  module RspecRequestHelpers
2
- VERSION = "0.2.3"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -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 'factory_bot'
37
+ spec.add_runtime_dependency 'rspec', '>= 2.4'
39
38
 
40
- spec.add_dependency 'rspec', '~> 3.0'
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.2.3
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-03-26 00:00:00.000000000 Z
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: factory_bot
56
+ name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
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: '0'
68
+ version: '2.4'
69
69
  - !ruby/object:Gem::Dependency
70
- name: rspec
70
+ name: rack
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '3.0'
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: '3.0'
82
+ version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: rack
84
+ name: bundler
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '2.0'
90
- type: :runtime
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: '2.0'
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: bundler
98
+ name: rspec
113
99
  requirement: !ruby/object:Gem::Requirement
114
100
  requirements:
115
101
  - - "~>"
116
102
  - !ruby/object:Gem::Version
117
- version: '1.16'
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: '1.16'
110
+ version: '3.0'
125
111
  - !ruby/object:Gem::Dependency
126
112
  name: rake
127
113
  requirement: !ruby/object:Gem::Requirement