Skip to content

Commit fb35b36

Browse files
committed
Basic spec check the API
1 parent 2eb221c commit fb35b36

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

.rspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--require spec_helper

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
22

33
# Specify your gem's dependencies in http-exceptions.gemspec
44
gemspec
5+
6+
gem 'rspec'

spec/http_exception_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'spec_helper'
2+
3+
describe Http::Exceptions do
4+
let(:invalid_response) { double(code: 400, body: '') }
5+
let(:valid_response) { double(code: 200) }
6+
7+
class TestException < RuntimeError
8+
end
9+
10+
describe ".wrap_exception" do
11+
it "raises an HttpException on supported http exceptions" do
12+
expect do
13+
described_class.wrap_exception do
14+
raise SocketError
15+
end
16+
end.to raise_error(Http::Exceptions::HttpException)
17+
end
18+
19+
it "ignores other exceptions" do
20+
expect do
21+
described_class.wrap_exception do
22+
raise TestException
23+
end
24+
end.to raise_error(TestException)
25+
end
26+
end
27+
28+
describe ".check_response!" do
29+
it "raises exception on non-200 response" do
30+
expect do
31+
described_class.check_response!(invalid_response)
32+
end.to raise_error(Http::Exceptions::HttpException)
33+
end
34+
35+
it "returns the response on valid response" do
36+
expect(described_class.check_response!(valid_response)).to eq(valid_response)
37+
end
38+
end
39+
40+
describe ".wrap_and_check" do
41+
it "raises exception on bad response" do
42+
expect do
43+
described_class.wrap_and_check do
44+
invalid_response
45+
end
46+
end.to raise_error(Http::Exceptions::HttpException)
47+
end
48+
end
49+
end

spec/spec_helper.rb

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
require 'http/exceptions'
2+
3+
# This file was generated by the `rspec --init` command. Conventionally, all
4+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
6+
# file to always be loaded, without a need to explicitly require it in any files.
7+
#
8+
# Given that it is always loaded, you are encouraged to keep this file as
9+
# light-weight as possible. Requiring heavyweight dependencies from this file
10+
# will add to the boot time of your test suite on EVERY test run, even for an
11+
# individual file that may not need all of that loaded. Instead, consider making
12+
# a separate helper file that requires the additional dependencies and performs
13+
# the additional setup, and require it from the spec files that actually need it.
14+
#
15+
# The `.rspec` file also contains a few flags that are not defaults but that
16+
# users commonly want.
17+
#
18+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19+
RSpec.configure do |config|
20+
# rspec-expectations config goes here. You can use an alternate
21+
# assertion/expectation library such as wrong or the stdlib/minitest
22+
# assertions if you prefer.
23+
config.expect_with :rspec do |expectations|
24+
# This option will default to `true` in RSpec 4. It makes the `description`
25+
# and `failure_message` of custom matchers include text for helper methods
26+
# defined using `chain`, e.g.:
27+
# be_bigger_than(2).and_smaller_than(4).description
28+
# # => "be bigger than 2 and smaller than 4"
29+
# ...rather than:
30+
# # => "be bigger than 2"
31+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32+
end
33+
34+
# rspec-mocks config goes here. You can use an alternate test double
35+
# library (such as bogus or mocha) by changing the `mock_with` option here.
36+
config.mock_with :rspec do |mocks|
37+
# Prevents you from mocking or stubbing a method that does not exist on
38+
# a real object. This is generally recommended, and will default to
39+
# `true` in RSpec 4.
40+
mocks.verify_partial_doubles = true
41+
end
42+
43+
# The settings below are suggested to provide a good initial experience
44+
# with RSpec, but feel free to customize to your heart's content.
45+
=begin
46+
# These two settings work together to allow you to limit a spec run
47+
# to individual examples or groups you care about by tagging them with
48+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
49+
# get run.
50+
config.filter_run :focus
51+
config.run_all_when_everything_filtered = true
52+
53+
# Limits the available syntax to the non-monkey patched syntax that is recommended.
54+
# For more details, see:
55+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
56+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
57+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
58+
config.disable_monkey_patching!
59+
60+
# This setting enables warnings. It's recommended, but in some cases may
61+
# be too noisy due to issues in dependencies.
62+
config.warnings = true
63+
64+
# Many RSpec users commonly either run the entire suite or an individual
65+
# file, and it's useful to allow more verbose output when running an
66+
# individual spec file.
67+
if config.files_to_run.one?
68+
# Use the documentation formatter for detailed output,
69+
# unless a formatter has already been configured
70+
# (e.g. via a command-line flag).
71+
config.default_formatter = 'doc'
72+
end
73+
74+
# Print the 10 slowest examples and example groups at the
75+
# end of the spec run, to help surface which specs are running
76+
# particularly slow.
77+
config.profile_examples = 10
78+
79+
# Run specs in random order to surface order dependencies. If you find an
80+
# order dependency and want to debug it, you can fix the order by providing
81+
# the seed, which is printed after each run.
82+
# --seed 1234
83+
config.order = :random
84+
85+
# Seed global randomization in this process using the `--seed` CLI option.
86+
# Setting this allows you to use `--seed` to deterministically reproduce
87+
# test failures related to randomization by passing the same `--seed` value
88+
# as the one that triggered the failure.
89+
Kernel.srand config.seed
90+
=end
91+
end

0 commit comments

Comments
 (0)