rspec_api_helpers 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f6d608d1fed495e82a37954164f31ac9b4b5eb36
4
+ data.tar.gz: eddba7eda1887a1d18465803783253e5d931806c
5
+ SHA512:
6
+ metadata.gz: e34e272f700e891486e4648b068e662ba8ca220a240c5912f765802dca1096460b84e501fa7aae97839d111b64ccf4a1c894e97021c7e1660463b98570a46105
7
+ data.tar.gz: d653994bbad3645ff0becb1f118a4912e0106d7a5459db55ece71e175c98f0d962646aa3ca1306f91bdc09659b2faac0b67fc091e0014c38cbf0f09525e57589
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ before_install:
3
+ - gem update --system
4
+ install: 'bundle install'
5
+ script: 'bundle exec rspec --format documentation'
6
+ rvm:
7
+ - '2.1.4'
8
+ - '2.0.0'
9
+ - '1.9.3'
10
+ - jruby-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rspec_api_helpers.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ian Ker-Seymer
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,74 @@
1
+ RspecApiHelpers
2
+ ===============
3
+ [![Build Status](https://travis-ci.org/sweatshirtio/rspec_api_helpers.svg?branch=master)](https://travis-ci.org/sweatshirtio/rspec_api_helpers)
4
+
5
+ This is a Gem to help you with all of you Rails API testing woes. The goal is
6
+ to provide functionality to make testing APIs a breeze. I hope you like it!
7
+
8
+ Add this to your Gemfile:
9
+
10
+ gem 'rspec_api_helpers'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install rspec_api_helpers
19
+
20
+ Include it in your spec_helper
21
+
22
+ ```ruby
23
+ RSpec.configure do |config|
24
+ # ...
25
+ config.include RspecApiHelpers
26
+ # ...
27
+ end
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ 1. Built it strategy for coercing FactoryGirl factories into json:
33
+
34
+ ```ruby
35
+ FactoryGirl.json :user, name: 'Mrs. Mock', email: 'test@test.com'
36
+ # => {\"name\":\"Mrs. Mock\",\"email\":\"test@test.com\"}"
37
+ ```
38
+
39
+ 2. Ready-to-fire headers:
40
+
41
+ ```ruby
42
+ headers_for :json
43
+ # => { 'CONTENT_TYPE' => 'application/json', 'ACCEPT' => 'application/json' }
44
+ ```
45
+
46
+ 3. Automatic JSON parsing for `response.body`:
47
+
48
+ ```ruby
49
+ # Call this in your spec
50
+ response_body
51
+ ```
52
+
53
+ 4. Hash with indifferent access by default when parsing JSON:
54
+
55
+ ```ruby
56
+ parse_json response.body
57
+ # => can now use user[:email], etc.
58
+ ```
59
+
60
+ ## Installation
61
+
62
+ Add this line to your application's Gemfile:
63
+
64
+ ```ruby
65
+ gem 'rspec_api_helpers'
66
+ ```
67
+
68
+ ## Contributing
69
+
70
+ 1. Fork it ( https://github.com/sweatshirtio/rspec_api_helpers/fork )
71
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
72
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
73
+ 4. Push to the branch (`git push origin my-new-feature`)
74
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,17 @@
1
+ require 'rspec_api_helpers/version'
2
+ require 'rspec_api_helpers/headers'
3
+ require 'rspec_api_helpers/json'
4
+ require 'rspec_api_helpers/strategies/json_strategy'
5
+
6
+ require 'active_support/core_ext/object/json'
7
+ require 'active_support/core_ext/hash'
8
+
9
+ module RspecApiHelpers
10
+ def self.included(base)
11
+ constants.map(&method(:const_get)).each do |const|
12
+ base.include const if const.class == Module
13
+ end
14
+ end
15
+ end
16
+
17
+ FactoryGirl.register_strategy :json, RspecApiHelpers::Strategies::JsonStrategy
@@ -0,0 +1,16 @@
1
+ module RspecApiHelpers
2
+ module Headers
3
+ def headers_for(type)
4
+ _headers.send type
5
+ end
6
+
7
+ def _headers
8
+ headers = OpenStruct.new
9
+ headers.json = {
10
+ 'CONTENT_TYPE' => 'application/json',
11
+ 'ACCEPT' => 'application/json'
12
+ }
13
+ headers
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ module RspecApiHelpers
2
+ module Json
3
+ def response_body
4
+ @response_body ||= parse_json response.body
5
+ end
6
+
7
+ def parse_json(json)
8
+ ActiveSupport::HashWithIndifferentAccess.new(ActiveSupport::JSON.decode json)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ require 'factory_girl'
2
+
3
+ module RspecApiHelpers
4
+ module Strategies
5
+ class JsonStrategy
6
+ def initialize
7
+ @strategy = FactoryGirl.strategy_by_name(:build).new
8
+ end
9
+
10
+ delegate :association, to: :@strategy
11
+
12
+ def result(evaluation)
13
+ @strategy.result(evaluation).to_json
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module RspecApiHelpers
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,27 @@
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 = RspecApiHelpers::VERSION
9
+ spec.authors = ["Ian Ker-Seymer"]
10
+ spec.email = ["i.kerseymer@gmail.com"]
11
+ spec.summary = %q{Helpers for testing Rails APIs.}
12
+ spec.homepage = "https://github.com/ianks/rspec_api_helpers"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "activesupport", "~> 4.0"
21
+ spec.add_dependency "factory_girl", "~> 4.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.1"
26
+ spec.add_development_dependency "pry"
27
+ end
@@ -0,0 +1,17 @@
1
+ describe RspecApiHelpers::Headers do
2
+ describe '.headers_for' do
3
+ describe :json do
4
+ subject { headers_for :json}
5
+
6
+ it {is_expected.to be_a Hash }
7
+
8
+ it 'should return CONTENT_TYPE' do
9
+ expect(subject['CONTENT_TYPE']).to eq 'application/json'
10
+ end
11
+
12
+ it 'should return ACCEPT' do
13
+ expect(subject['ACCEPT']).to eq 'application/json'
14
+ end
15
+ end
16
+ end
17
+ end
data/spec/json_spec.rb ADDED
@@ -0,0 +1,40 @@
1
+ describe RspecApiHelpers::Json do
2
+ let(:data) do
3
+ <<-json
4
+ {
5
+ "data" : "is good",
6
+ "email" : "test@test.com",
7
+ "location" : {
8
+ "city" : "Somewhere",
9
+ "state" : "Cool"
10
+ }
11
+ }
12
+ json
13
+ end
14
+
15
+ let(:response) { Class.new }
16
+
17
+ subject { parse_json data }
18
+
19
+ describe '.parse_json' do
20
+ it 'should have indifferent access' do
21
+ expect(subject[:email]).to eq 'test@test.com'
22
+ end
23
+
24
+ it 'should have indifferent access (nested)' do
25
+ expect(subject[:location][:city]).to eq 'Somewhere'
26
+ end
27
+ end
28
+
29
+ describe '.response_body' do
30
+ before { allow(response).to receive(:body) { data } }
31
+
32
+ it 'should return a hash' do
33
+ expect(response_body).to be_a Hash
34
+ end
35
+
36
+ it 'return correct data' do
37
+ expect(response_body[:email]).to eq 'test@test.com'
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,18 @@
1
+ require 'rspec_api_helpers'
2
+
3
+ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
4
+
5
+ RSpec.configure do |config|
6
+ config.expect_with :rspec do |expectations|
7
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
8
+ end
9
+
10
+ config.warnings = true
11
+ config.default_formatter = 'doc' if config.files_to_run.one?
12
+ config.order = :random
13
+
14
+ Kernel.srand config.seed
15
+
16
+ config.include RspecApiHelpers::Json
17
+ config.include RspecApiHelpers::Headers
18
+ end
@@ -0,0 +1,18 @@
1
+ describe RspecApiHelpers::Strategies::JsonStrategy do
2
+ before do
3
+ User = Class.new { attr_accessor :name, :email }
4
+
5
+ FactoryGirl.define do
6
+ factory :user do
7
+ email 'test@test.com'
8
+ name 'Mrs. Mock'
9
+ end
10
+ end
11
+ end
12
+
13
+ subject { FactoryGirl.json :user }
14
+
15
+ it 'should render user as json' do
16
+ expect(subject).to eq '{"email":"test@test.com","name":"Mrs. Mock"}'
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec_api_helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ian Ker-Seymer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: factory_girl
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - i.kerseymer@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - lib/rspec_api_helpers.rb
112
+ - lib/rspec_api_helpers/headers.rb
113
+ - lib/rspec_api_helpers/json.rb
114
+ - lib/rspec_api_helpers/strategies/json_strategy.rb
115
+ - lib/rspec_api_helpers/version.rb
116
+ - rspec_api_helpers.gemspec
117
+ - spec/headers_spec.rb
118
+ - spec/json_spec.rb
119
+ - spec/spec_helper.rb
120
+ - spec/strategies_spec.rb
121
+ homepage: https://github.com/ianks/rspec_api_helpers
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.2.2
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Helpers for testing Rails APIs.
145
+ test_files:
146
+ - spec/headers_spec.rb
147
+ - spec/json_spec.rb
148
+ - spec/spec_helper.rb
149
+ - spec/strategies_spec.rb
150
+ has_rdoc: