rspec-api_helpers 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +63 -0
- data/Rakefile +2 -0
- data/lib/rspec/api_helpers/example_group_methods.rb +58 -0
- data/lib/rspec/api_helpers/example_methods.rb +50 -0
- data/lib/rspec/api_helpers/version.rb +5 -0
- data/lib/rspec/api_helpers.rb +12 -0
- data/rspec-api_helpers.gemspec +23 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1059a5b7da87e98a2292e3225d9c7eb3d686c8c1
|
4
|
+
data.tar.gz: 01ea35dc008e61c27fd7a84088c3773c790b2fb9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 702e77dfb79183d12f464760f5b969555f3dcb6734b77c0def10a7773b098791b4a8377c8c977f7b4e5ed0c8ea29fa5e7a0f754ab83d0e6aff7798712150e663
|
7
|
+
data.tar.gz: ab883d0ce0e862788ef40512c239c955ba14ec9770332bfc74485619832385aefe84a845031f4e887d73eef12c83bf5e8343edd9b4188ee5e08816efb6dfd6a7
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Filippos Vasilakis
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Rspec::ApiHelpers
|
2
|
+
|
3
|
+
Usefull Rspec helpers for APIs (currently only ActiveModel Serializers are supported)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'rspec-api_helpers', github: 'kollegorna/rspec-api_helpers'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install rspec-api_helpers
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
This Gem expects you to have set your rspec to use Rake::Test helpers as described
|
23
|
+
[here](https://gist.github.com/alex-zige/5795358) because it checks `last_response`
|
24
|
+
attributes.
|
25
|
+
|
26
|
+
### Examples
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
it_returns_status(200)
|
30
|
+
```
|
31
|
+
It checks if the HTTP response status is 200.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
it_returns_attributes(resource: 'user', model: '@user', only: [
|
35
|
+
:email, :name
|
36
|
+
])
|
37
|
+
```
|
38
|
+
It checks if the HTTP body contains an AMS json that has :email and :name attributes and
|
39
|
+
compares them with '@user' variable's attributes.
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
it_returns_more_attributes(
|
43
|
+
resource: 'user',
|
44
|
+
model: 'User.last!',
|
45
|
+
only: [:updated_at, :created_at],
|
46
|
+
modifier: 'iso8601'
|
47
|
+
)
|
48
|
+
```
|
49
|
+
It checks if the HTTP body contains an AMS json that has :updated_at and :created_at
|
50
|
+
attributes and compares them with '@user' variable's attributes after it applies modifier.
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
it_returns_resources(root: 'users', number: 5)
|
54
|
+
```
|
55
|
+
It checks if the HTTP body contains an AMS json with an array of 'users'.
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
1. Fork it ( https://github.com/[my-github-username]/rspec-api_helpers/fork )
|
60
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
61
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
62
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
63
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
module Rspec
|
2
|
+
module ApiHelpers
|
3
|
+
module ExampleGroupMethods
|
4
|
+
def it_returns_status(status)
|
5
|
+
it 'returns the correct status' do
|
6
|
+
expect(last_response.status).to eql(status)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def it_returns_attributes(resource:, model:, only: [], modifier: nil)
|
11
|
+
it "expects returned resource to have the following model's attributes" do
|
12
|
+
@api_resource = objectize_resource(last_response.body, root: resource)
|
13
|
+
|
14
|
+
@model = eval(model)
|
15
|
+
if @model.is_a? Hash
|
16
|
+
@model = object_hash(@model)
|
17
|
+
end
|
18
|
+
|
19
|
+
if only
|
20
|
+
only.each do |attribute|
|
21
|
+
begin
|
22
|
+
if modifier
|
23
|
+
expect(@api_resource.send(attribute)).to(
|
24
|
+
eql(@model.send(attribute).send(modifier.to_sym))
|
25
|
+
)
|
26
|
+
else
|
27
|
+
expect(@api_resource.send(attribute)).to eql(@model.send(attribute))
|
28
|
+
end
|
29
|
+
rescue RSpec::Expectations::ExpectationNotMetError => e
|
30
|
+
e.message << "failed at model attribute: #{attribute}"
|
31
|
+
raise e
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
alias_method :it_returns_db_model, :it_returns_attributes
|
39
|
+
alias_method :it_returns_more_attributes, :it_returns_attributes
|
40
|
+
|
41
|
+
def it_includes_in_headers(headers = {})
|
42
|
+
it 'returns the correct headers' do
|
43
|
+
headers.each do |header, value|
|
44
|
+
expect(last_response.headers[header.to_s]).to eq(eval(value))
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def it_returns_resources(root:, number:)
|
50
|
+
it 'returns the correct number of data in the body' do
|
51
|
+
users = objectize_resources(last_response.body, root: root)
|
52
|
+
expect(users.length).to eql(number)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
alias_method :it_returns_the_resources, :it_returns_resources
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "rspec/api_helpers/version"
|
2
|
+
|
3
|
+
module Rspec
|
4
|
+
module ApiHelpers
|
5
|
+
module ExampleMethods
|
6
|
+
def objectize_resources(json, root:)
|
7
|
+
array = []
|
8
|
+
array_hash = HashWithIndifferentAccess.new(JSON.load(json))
|
9
|
+
|
10
|
+
if root
|
11
|
+
array_hash = array_hash[root]
|
12
|
+
end
|
13
|
+
|
14
|
+
array_hash.each do |resource|
|
15
|
+
array << object_hash(resource)
|
16
|
+
end
|
17
|
+
|
18
|
+
return array
|
19
|
+
end
|
20
|
+
|
21
|
+
def objectize_resource(json, root:)
|
22
|
+
hash = HashWithIndifferentAccess.new(JSON.load(json))
|
23
|
+
if root
|
24
|
+
obj = object_hash(hash[root])
|
25
|
+
else
|
26
|
+
obj = object_hash(hash)
|
27
|
+
end
|
28
|
+
|
29
|
+
return obj
|
30
|
+
end
|
31
|
+
|
32
|
+
def object_hash(hash)
|
33
|
+
ObjectHash.new(hash)
|
34
|
+
end
|
35
|
+
|
36
|
+
class ObjectHash
|
37
|
+
attr_accessor :hash
|
38
|
+
def initialize(hash)
|
39
|
+
@hash = HashWithIndifferentAccess.new(hash)
|
40
|
+
end
|
41
|
+
def method_missing(name)
|
42
|
+
return hash[name] if hash.key? name
|
43
|
+
raise KeyError.new("Attribute not found in resource: #{name}")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rspec/api_helpers/version'
|
2
|
+
require 'rspec/api_helpers/example_methods'
|
3
|
+
require 'rspec/api_helpers/example_group_methods'
|
4
|
+
|
5
|
+
module Rspec
|
6
|
+
module ApiHelpers
|
7
|
+
def self.included(receiver)
|
8
|
+
receiver.extend ExampleGroupMethods
|
9
|
+
receiver.send :include, ExampleMethods
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rspec/api_helpers/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rspec-api_helpers"
|
8
|
+
spec.version = Rspec::Api::VERSION
|
9
|
+
spec.authors = ["Filippos Vasilakis"]
|
10
|
+
spec.email = ["vasilakisfil@gmail.com"]
|
11
|
+
spec.summary = %q{Rspec matchers for APIs}
|
12
|
+
spec.description = %q{Rspec matchers for APIs}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rspec-api_helpers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Filippos Vasilakis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Rspec matchers for APIs
|
42
|
+
email:
|
43
|
+
- vasilakisfil@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- lib/rspec/api_helpers.rb
|
54
|
+
- lib/rspec/api_helpers/example_group_methods.rb
|
55
|
+
- lib/rspec/api_helpers/example_methods.rb
|
56
|
+
- lib/rspec/api_helpers/version.rb
|
57
|
+
- rspec-api_helpers.gemspec
|
58
|
+
homepage: ''
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.4.5
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Rspec matchers for APIs
|
82
|
+
test_files: []
|