Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Grape::Endpoint.setup for pre-run endpoint config. #397

Merged
merged 1 commit into from
Apr 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Next Release
#### Features

* [#617](https://github.com/intridea/grape/pull/617): Running tests on Ruby 2.1.1, Rubinius 2.1 and 2.2, Ruby and JRuby HEAD - [@dblock](https://github.com/dblock).
* [#397](https://github.com/intridea/grape/pull/397): Adds `Grape::Endpoint.before_each` to allow easy helper stubbing - [@mbleigh](https://github.com/mbleigh).
* Your contribution here.

#### Fixes
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
- [Writing Tests](#writing-tests)
- [Writing Tests with Rack](#writing-tests-with-rack)
- [Writing Tests with Rails](#writing-tests-with-rails)
- [Stubbing Helpers](#stubbing-helpers)
- [Reloading API Changes in Development](#reloading-api-changes-in-development)
- [Rails 3.x](#rails-3x)
- [Performance Monitoring](#performance-monitoring)
Expand Down Expand Up @@ -1606,6 +1607,31 @@ RSpec.configure do |config|
end
```

### Stubbing Helpers

Because helpers are mixed in based on the context when an endpoint is defined, it can
be difficult to stub or mock them for testing. The `Grape::Endpoint.before_each` method
can help by allowing you to define behavior on the endpoint that will run before every
request.

```ruby
describe 'an endpoint that needs helpers stubbed' do
before do
Grape::Endpoint.before_each do |endpoint|
endpoint.stub!(:helper_name).and_return('desired_value')
end
end

after do
Grape::Endpoint.before_each nil
end

it 'should properly stub the helper' do
# ...
end
end
```

## Reloading API Changes in Development

### Rails 3.x
Expand Down
14 changes: 14 additions & 0 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ class Endpoint
attr_reader :env, :request, :headers, :params

class << self
def before_each(new_setup = false, &block)
if new_setup == false
if block_given?
@before_each = block
else
return @before_each
end
else
@before_each = new_setup
end
end

# @api private
#
# Create an UnboundMethod that is appropriate for executing an endpoint
Expand Down Expand Up @@ -383,6 +395,8 @@ def run(env)

cookies.read(@request)

self.class.before_each.call(self) if self.class.before_each

run_filters befores

run_filters before_validations
Expand Down
31 changes: 31 additions & 0 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,37 @@ def app
subject
end

describe '.before_each' do
after { Grape::Endpoint.before_each(nil) }

it 'should be settable via block' do
block = lambda { |endpoint| "noop" }
Grape::Endpoint.before_each(&block)
expect(Grape::Endpoint.before_each).to eq(block)
end

it 'should be settable via reference' do
block = lambda { |endpoint| "noop" }
Grape::Endpoint.before_each block
expect(Grape::Endpoint.before_each).to eq(block)
end

it 'should be able to override a helper' do
subject.get("/") { current_user }
expect { get '/' }.to raise_error(NameError)

Grape::Endpoint.before_each do |endpoint|
endpoint.stub(:current_user).and_return("Bob")
end

get '/'
expect(last_response.body).to eq("Bob")

Grape::Endpoint.before_each(nil)
expect { get '/' }.to raise_error(NameError)
end
end

describe '#initialize' do
it 'takes a settings stack, options, and a block' do
p = proc {}
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'bundler'
Bundler.setup :default, :test

require 'json'
require 'rack/test'
require 'base64'
require 'cookiejar'
Expand Down