Skip to content

Commit

Permalink
Adds Grape::Endpoint.before_each for pre-run endpoint config. Refs #396
Browse files Browse the repository at this point in the history
  • Loading branch information
mbleigh committed Apr 12, 2014
1 parent 7f6f999 commit bd9265c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
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

0 comments on commit bd9265c

Please sign in to comment.