rspec_request_helpers 0.1.1 → 0.2.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 +8 -1
- data/README.md +39 -6
- data/lib/rspec_request_helpers/helpers.rb +39 -0
- data/lib/rspec_request_helpers/version.rb +1 -1
- data/rspec_request_helpers.gemspec +19 -17
- metadata +30 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6123bd35683c01320a81b8b564d8dd518298489c2063be3ea2b83460cde16153
|
4
|
+
data.tar.gz: 595d6b0cede37d70e8e0a6fabc2efae031c1cb33d2a737ed7bc49391a76ab5c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 552e36a4241ab85cc6933ced18d29b315fee8e7436a5b98f1cedc071981d5c94e0b46400f0a05bc9a7642a3c3c6346c69cd48482c0f161002d26dd8886005238
|
7
|
+
data.tar.gz: 0329fc125a3c35e2477b5d91e8908658c77c36c9fef6dd382b94964c46e151c21344981309b6ef80be9c9f9c61aff20414382197ec072434a790de21cd4fafca
|
data/Gemfile.lock
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
rspec_request_helpers (0.
|
4
|
+
rspec_request_helpers (0.2.0)
|
5
5
|
actionpack (>= 3.0)
|
6
6
|
activesupport (>= 3.0)
|
7
|
+
factory_bot
|
8
|
+
parser
|
7
9
|
rack (~> 2.0)
|
8
10
|
railties (>= 3.0)
|
9
11
|
rspec (~> 3.0)
|
@@ -29,12 +31,15 @@ GEM
|
|
29
31
|
i18n (>= 0.7, < 2)
|
30
32
|
minitest (~> 5.1)
|
31
33
|
tzinfo (~> 1.1)
|
34
|
+
ast (2.4.0)
|
32
35
|
builder (3.2.3)
|
33
36
|
coderay (1.1.2)
|
34
37
|
concurrent-ruby (1.0.5)
|
35
38
|
crass (1.0.4)
|
36
39
|
diff-lcs (1.3)
|
37
40
|
erubi (1.7.1)
|
41
|
+
factory_bot (4.11.1)
|
42
|
+
activesupport (>= 3.0.0)
|
38
43
|
i18n (1.1.1)
|
39
44
|
concurrent-ruby (~> 1.0)
|
40
45
|
loofah (2.2.2)
|
@@ -45,6 +50,8 @@ GEM
|
|
45
50
|
minitest (5.11.3)
|
46
51
|
nokogiri (1.8.5)
|
47
52
|
mini_portile2 (~> 2.3.0)
|
53
|
+
parser (2.5.1.2)
|
54
|
+
ast (~> 2.4.0)
|
48
55
|
pry (0.11.3)
|
49
56
|
coderay (~> 1.1.0)
|
50
57
|
method_source (~> 0.9.0)
|
data/README.md
CHANGED
@@ -9,13 +9,32 @@ In order to use it in your `rspec/requests` specs you need to follow one rule:
|
|
9
9
|
NOTE: version 0.1.1 introduced the helpers which ensure naming conventions. They have identical names and in fact are
|
10
10
|
wrappers around RSpec `let` functionality.
|
11
11
|
|
12
|
-
So what you'll got for that? Few handy methods for the testing routine
|
12
|
+
So what you'll got for that? - Few handy methods for the testing routine
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
| Action | Meaning | RSpec Example |
|
15
|
+
|---|---|---|
|
16
|
+
| `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)` |
|
17
|
+
| `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` |
|
18
|
+
| `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)` |
|
19
|
+
|
20
|
+
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.
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
#
|
24
|
+
# Usage:
|
25
|
+
# user(:user, name: 'Bob') translates to let(:user) { FactoryBot.create(:user, name: 'Bob') }
|
26
|
+
# user!(:user, name: 'Bob') translates to let!(:user) { FactoryBot.create(:user, name: 'Bob') }
|
27
|
+
# user_list(:user, name: 'Bob', 3) translates to let(:user) { FactoryBot.create_list(:user, 3, name: 'Bob') }
|
28
|
+
# user_list!(:user, name: 'Bob', 3) translates to let!(:user) { FactoryBot.create_list(:user, 3, name: 'Bob') }
|
29
|
+
#
|
30
|
+
database do
|
31
|
+
company2!(:company, name: name, domain_name: domain, location: location_name)
|
32
|
+
company3!(:company, name: name2, domain_name: domain, location: location_name)
|
33
|
+
order1!(:order, :ordered, :with_items, items_count: 1, status: IN_QC, company_id: company3.id, delivered_at: Time.zone.now)
|
34
|
+
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))
|
35
|
+
order3!(:order, :ordered, :with_items, items_count: 1, status: NOT_READY, company_id: company3.id)
|
36
|
+
end
|
37
|
+
```
|
19
38
|
|
20
39
|
## Installation
|
21
40
|
|
@@ -54,6 +73,20 @@ RSpec.configure do |config|
|
|
54
73
|
end
|
55
74
|
```
|
56
75
|
|
76
|
+
## Configuration
|
77
|
+
|
78
|
+
Create file `config/initializers/rspec_request_helpers.rb`
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
RspecRequestHelpers.configure do |config|
|
82
|
+
# Supporded content types
|
83
|
+
config.content_types = { json: 'application/json' }
|
84
|
+
|
85
|
+
# Supported status codes
|
86
|
+
config.status_codes = [404, 401, 422, 200, 201]
|
87
|
+
end
|
88
|
+
```
|
89
|
+
|
57
90
|
Generate new file for API endpoint
|
58
91
|
|
59
92
|
$ rails g rspec:endpoint show api/v1/users
|
@@ -1,6 +1,45 @@
|
|
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
|
+
|
4
43
|
def path(&block)
|
5
44
|
let(:path, &block)
|
6
45
|
end
|
@@ -1,33 +1,33 @@
|
|
1
1
|
|
2
|
-
lib = File.expand_path(
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
4
|
+
require 'rspec_request_helpers/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'rspec_request_helpers'
|
8
8
|
spec.version = RspecRequestHelpers::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
9
|
+
spec.authors = ['Andrii Baran']
|
10
|
+
spec.email = ['andriy.baran.v@gmail.com']
|
11
11
|
|
12
12
|
spec.summary = %q{A set of helpers for request test with RSpec}
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
13
|
+
spec.homepage = 'https://github.com/andriy-baran/rspec_request_helpers'
|
14
|
+
spec.license = 'MIT'
|
15
15
|
|
16
16
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
17
|
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
18
|
if spec.respond_to?(:metadata)
|
19
|
-
spec.metadata[
|
19
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
20
20
|
else
|
21
|
-
raise
|
22
|
-
|
21
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
22
|
+
'public gem pushes.'
|
23
23
|
end
|
24
24
|
|
25
25
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
26
26
|
f.match(%r{^(test|spec|features)/})
|
27
27
|
end
|
28
|
-
spec.bindir =
|
28
|
+
spec.bindir = 'exe'
|
29
29
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
-
spec.require_paths = [
|
30
|
+
spec.require_paths = ['lib']
|
31
31
|
spec.required_ruby_version = '>= 2.2.2'
|
32
32
|
|
33
33
|
version_string = ['>= 3.0']
|
@@ -35,11 +35,13 @@ Gem::Specification.new do |spec|
|
|
35
35
|
spec.add_runtime_dependency 'activesupport', version_string
|
36
36
|
spec.add_runtime_dependency 'actionpack', version_string
|
37
37
|
spec.add_runtime_dependency 'railties', version_string
|
38
|
+
spec.add_runtime_dependency 'factory_bot'
|
38
39
|
|
39
|
-
spec.add_dependency
|
40
|
-
spec.add_dependency
|
40
|
+
spec.add_dependency 'rspec', '~> 3.0'
|
41
|
+
spec.add_dependency 'rack', '~> 2.0'
|
42
|
+
spec.add_dependency 'parser'
|
41
43
|
|
42
|
-
spec.add_development_dependency
|
43
|
-
spec.add_development_dependency
|
44
|
-
spec.add_development_dependency
|
44
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
45
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
46
|
+
spec.add_development_dependency 'pry'
|
45
47
|
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.2.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: 2018-10-
|
11
|
+
date: 2018-10-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: factory_bot
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rspec
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,6 +94,20 @@ dependencies:
|
|
80
94
|
- - "~>"
|
81
95
|
- !ruby/object:Gem::Version
|
82
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'
|
83
111
|
- !ruby/object:Gem::Dependency
|
84
112
|
name: bundler
|
85
113
|
requirement: !ruby/object:Gem::Requirement
|