Skip to content

Commit 00dc6d8

Browse files
committed
Fix: namespaced test classes, closes #1165.
1 parent 210b4e1 commit 00dc6d8

14 files changed

+196
-166
lines changed

spec/grape/api/custom_validations_spec.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
describe Grape::Validations do
44
before do
5-
class DefaultLength < Grape::Validations::Base
6-
def validate_param!(attr_name, params)
7-
@option = params[:max].to_i if params.key?(:max)
8-
return if params[attr_name].length <= @option
9-
fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: "must be at the most #{@option} characters long"
5+
module CustomValidationsSpec
6+
class DefaultLength < Grape::Validations::Base
7+
def validate_param!(attr_name, params)
8+
@option = params[:max].to_i if params.key?(:max)
9+
return if params[attr_name].length <= @option
10+
fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: "must be at the most #{@option} characters long"
11+
end
1012
end
1113
end
1214
end

spec/grape/api/deeply_included_options_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'spec_helper'
22

3-
module API
3+
module DeeplyIncludedOptionsSpec
44
module Defaults
55
extend ActiveSupport::Concern
66
included do
@@ -11,11 +11,11 @@ module Defaults
1111
module Admin
1212
module Defaults
1313
extend ActiveSupport::Concern
14-
include API::Defaults
14+
include DeeplyIncludedOptionsSpec::Defaults
1515
end
1616

1717
class Users < Grape::API
18-
include API::Admin::Defaults
18+
include DeeplyIncludedOptionsSpec::Admin::Defaults
1919

2020
resource :users do
2121
get do
@@ -24,14 +24,14 @@ class Users < Grape::API
2424
end
2525
end
2626
end
27-
end
2827

29-
class Main < Grape::API
30-
mount API::Admin::Users
28+
class Main < Grape::API
29+
mount DeeplyIncludedOptionsSpec::Admin::Users
30+
end
3131
end
3232

3333
describe Grape::API do
34-
subject { Main }
34+
subject { DeeplyIncludedOptionsSpec::Main }
3535

3636
def app
3737
subject

spec/grape/api/nested_helpers_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'spec_helper'
22

33
describe Grape::API::Helpers do
4-
subject do
4+
module NestedHelpersSpec
55
module HelperMethods
66
extend Grape::API::Helpers
77
def current_user
@@ -28,8 +28,10 @@ class Nested < Grape::API
2828
class Main < Grape::API
2929
mount Nested
3030
end
31+
end
3132

32-
Main
33+
subject do
34+
NestedHelpersSpec::Main
3335
end
3436

3537
def app

spec/grape/api/shared_helpers_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
require 'spec_helper'
22

33
describe Grape::API::Helpers do
4-
module SharedParams
5-
extend Grape::API::Helpers
4+
subject do
5+
shared_params = Module.new do
6+
extend Grape::API::Helpers
67

7-
params :pagination do
8-
optional :page, type: Integer
9-
optional :size, type: Integer
8+
params :pagination do
9+
optional :page, type: Integer
10+
optional :size, type: Integer
11+
end
1012
end
11-
end
1213

13-
subject do
1414
Class.new(Grape::API) do
15-
helpers SharedParams
15+
helpers shared_params
1616
format :json
1717

1818
params do

spec/grape/api_spec.rb

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,21 +1309,23 @@ def three
13091309

13101310
context 'CustomError subclass of Grape::Exceptions::Base' do
13111311
before do
1312-
class CustomError < Grape::Exceptions::Base; end
1312+
module ApiSpec
1313+
class CustomError < Grape::Exceptions::Base; end
1314+
end
13131315
end
13141316

13151317
it 'does not re-raise exceptions of type Grape::Exceptions::Base' do
1316-
subject.get('/custom_exception') { fail CustomError }
1318+
subject.get('/custom_exception') { fail ApiSpec::CustomError }
13171319

13181320
expect { get '/custom_exception' }.not_to raise_error
13191321
end
13201322

13211323
it 'rescues custom grape exceptions' do
1322-
subject.rescue_from CustomError do |e|
1324+
subject.rescue_from ApiSpec::CustomError do |e|
13231325
rack_response('New Error', e.status)
13241326
end
13251327
subject.get '/custom_error' do
1326-
fail CustomError, status: 400, message: 'Custom Error'
1328+
fail ApiSpec::CustomError, status: 400, message: 'Custom Error'
13271329
end
13281330

13291331
get '/custom_error'
@@ -1474,21 +1476,23 @@ def rescue_arg_error
14741476

14751477
describe '.rescue_from klass, rescue_subclasses: boolean' do
14761478
before do
1477-
module APIErrors
1478-
class ParentError < StandardError; end
1479-
class ChildError < ParentError; end
1479+
module ApiSpec
1480+
module APIErrors
1481+
class ParentError < StandardError; end
1482+
class ChildError < ParentError; end
1483+
end
14801484
end
14811485
end
14821486

14831487
it 'rescues error as well as subclass errors with rescue_subclasses option set' do
1484-
subject.rescue_from APIErrors::ParentError, rescue_subclasses: true do |e|
1488+
subject.rescue_from ApiSpec::APIErrors::ParentError, rescue_subclasses: true do |e|
14851489
rack_response("rescued from #{e.class.name}", 500)
14861490
end
14871491
subject.get '/caught_child' do
1488-
fail APIErrors::ChildError
1492+
fail ApiSpec::APIErrors::ChildError
14891493
end
14901494
subject.get '/caught_parent' do
1491-
fail APIErrors::ParentError
1495+
fail ApiSpec::APIErrors::ParentError
14921496
end
14931497
subject.get '/uncaught_parent' do
14941498
fail StandardError
@@ -1502,25 +1506,25 @@ class ChildError < ParentError; end
15021506
end
15031507

15041508
it 'sets rescue_subclasses to true by default' do
1505-
subject.rescue_from APIErrors::ParentError do |e|
1509+
subject.rescue_from ApiSpec::APIErrors::ParentError do |e|
15061510
rack_response("rescued from #{e.class.name}", 500)
15071511
end
15081512
subject.get '/caught_child' do
1509-
fail APIErrors::ChildError
1513+
fail ApiSpec::APIErrors::ChildError
15101514
end
15111515

15121516
get '/caught_child'
15131517
expect(last_response.status).to eql 500
15141518
end
15151519

15161520
it 'does not rescue child errors if rescue_subclasses is false' do
1517-
subject.rescue_from APIErrors::ParentError, rescue_subclasses: false do |e|
1521+
subject.rescue_from ApiSpec::APIErrors::ParentError, rescue_subclasses: false do |e|
15181522
rack_response("rescued from #{e.class.name}", 500)
15191523
end
15201524
subject.get '/uncaught' do
1521-
fail APIErrors::ChildError
1525+
fail ApiSpec::APIErrors::ChildError
15221526
end
1523-
expect { get '/uncaught' }.to raise_error(APIErrors::ChildError)
1527+
expect { get '/uncaught' }.to raise_error(ApiSpec::APIErrors::ChildError)
15241528
end
15251529
end
15261530

@@ -1572,15 +1576,17 @@ class ChildError < ParentError; end
15721576

15731577
context 'class' do
15741578
before :each do
1575-
class CustomErrorFormatter
1576-
def self.call(message, _backtrace, _options, _env)
1577-
"message: #{message} @backtrace"
1579+
module ApiSpec
1580+
class CustomErrorFormatter
1581+
def self.call(message, _backtrace, _options, _env)
1582+
"message: #{message} @backtrace"
1583+
end
15781584
end
15791585
end
15801586
end
15811587
it 'returns a custom error format' do
15821588
subject.rescue_from :all, backtrace: true
1583-
subject.error_formatter :txt, CustomErrorFormatter
1589+
subject.error_formatter :txt, ApiSpec::CustomErrorFormatter
15841590
subject.get '/exception' do
15851591
fail 'rain!'
15861592
end
@@ -1592,16 +1598,18 @@ def self.call(message, _backtrace, _options, _env)
15921598
describe 'with' do
15931599
context 'class' do
15941600
before :each do
1595-
class CustomErrorFormatter
1596-
def self.call(message, _backtrace, _option, _env)
1597-
"message: #{message} @backtrace"
1601+
module ApiSpec
1602+
class CustomErrorFormatter
1603+
def self.call(message, _backtrace, _option, _env)
1604+
"message: #{message} @backtrace"
1605+
end
15981606
end
15991607
end
16001608
end
16011609

16021610
it 'returns a custom error format' do
16031611
subject.rescue_from :all, backtrace: true
1604-
subject.error_formatter :txt, with: CustomErrorFormatter
1612+
subject.error_formatter :txt, with: ApiSpec::CustomErrorFormatter
16051613
subject.get('/exception') { fail 'rain!' }
16061614

16071615
get '/exception'
@@ -1713,15 +1721,17 @@ def self.call(message, _backtrace, _option, _env)
17131721
end
17141722
end
17151723
context 'custom formatter class' do
1716-
module CustomFormatter
1717-
def self.call(object, _env)
1718-
"{\"custom_formatter\":\"#{object[:some]}\"}"
1724+
module ApiSpec
1725+
module CustomFormatter
1726+
def self.call(object, _env)
1727+
"{\"custom_formatter\":\"#{object[:some]}\"}"
1728+
end
17191729
end
17201730
end
17211731
before :each do
17221732
subject.content_type :json, 'application/json'
17231733
subject.content_type :custom, 'application/custom'
1724-
subject.formatter :custom, CustomFormatter
1734+
subject.formatter :custom, ApiSpec::CustomFormatter
17251735
subject.get :simple do
17261736
{ some: 'hash' }
17271737
end
@@ -1765,15 +1775,17 @@ def self.call(object, _env)
17651775
end
17661776
end
17671777
context 'custom parser class' do
1768-
module CustomParser
1769-
def self.call(object, _env)
1770-
{ object.to_sym => object.to_s.reverse }
1778+
module ApiSpec
1779+
module CustomParser
1780+
def self.call(object, _env)
1781+
{ object.to_sym => object.to_s.reverse }
1782+
end
17711783
end
17721784
end
17731785
before :each do
17741786
subject.content_type :txt, 'text/plain'
17751787
subject.content_type :custom, 'text/custom'
1776-
subject.parser :custom, CustomParser
1788+
subject.parser :custom, ApiSpec::CustomParser
17771789
subject.put :simple do
17781790
params[:simple]
17791791
end

spec/grape/entity_spec.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,24 @@ def app
4545
entity = Class.new(Grape::Entity)
4646
allow(entity).to receive(:represent).and_return('Hiya')
4747

48-
class TestObject
49-
end
48+
module EntitySpec
49+
class TestObject
50+
end
5051

51-
class FakeCollection
52-
def first
53-
TestObject.new
52+
class FakeCollection
53+
def first
54+
TestObject.new
55+
end
5456
end
5557
end
5658

57-
subject.represent TestObject, with: entity
59+
subject.represent EntitySpec::TestObject, with: entity
5860
subject.get '/example' do
59-
present [TestObject.new]
61+
present [EntitySpec::TestObject.new]
6062
end
6163

6264
subject.get '/example2' do
63-
present FakeCollection.new
65+
present EntitySpec::FakeCollection.new
6466
end
6567

6668
get '/example'

spec/grape/middleware/base_spec.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,20 @@
8282
end
8383

8484
context 'defaults' do
85-
class ExampleWare < Grape::Middleware::Base
86-
def default_options
87-
{ monkey: true }
85+
module BaseSpec
86+
class ExampleWare < Grape::Middleware::Base
87+
def default_options
88+
{ monkey: true }
89+
end
8890
end
8991
end
9092

9193
it 'persists the default options' do
92-
expect(ExampleWare.new(blank_app).options[:monkey]).to be true
94+
expect(BaseSpec::ExampleWare.new(blank_app).options[:monkey]).to be true
9395
end
9496

9597
it 'overrides default options when provided' do
96-
expect(ExampleWare.new(blank_app, monkey: false).options[:monkey]).to be false
98+
expect(BaseSpec::ExampleWare.new(blank_app, monkey: false).options[:monkey]).to be false
9799
end
98100
end
99101
end

0 commit comments

Comments
 (0)