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

allow stubbing of helper methods #398

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,37 @@ RSpec.configure do |config|
end
```

### Mocking and Stubbing helper methods

helpers are defined on a helper object which can be stubbed out.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor:

  • Text should start with a capital H.
  • Simplify the example. I would keep only one thing and remove the describe parts and the require, just to get exactly to the point of how to stub something.


```ruby
require 'spec_helper'

describe Twitter::API do
describe "logged in behaviour" do
it "returns an the user's statuses" do
user = double(statuses: [1])
Twitter::API.helper.stub(:current_user).and_return(user)
get "/api/v1/statuses"
response.status.should == 200
JSON.parse(response.body).should == [1]
end
end

describe "logged out behaviour" do

it "returns an empty array when user's logged out" do
Twitter::API.helper.stub(:current_user).and_return(nil)
get "/api/v1/statuses"
response.status.should == 200
JSON.parse(response.body).should == []
end
end
end
````


## Reloading API Changes in Development

### Rails 3.x
Expand Down
1 change: 1 addition & 0 deletions lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
I18n.load_path << File.expand_path('../grape/locale/en.yml', __FILE__)

module Grape
autoload :HelperObject, 'grape/helper_object'
autoload :API, 'grape/api'
autoload :Endpoint, 'grape/endpoint'
autoload :Route, 'grape/route'
Expand Down
10 changes: 9 additions & 1 deletion lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ def helpers(new_mod = nil, &block)
change!
mod
end
helper.extend(mod)
mod
end

def helper
@helper ||= HelperObject.new
end

# Add an authentication type to the API. Currently
Expand Down Expand Up @@ -307,7 +313,8 @@ def mount(mounts)
endpoints << Grape::Endpoint.new(settings.clone, {
:method => :any,
:path => path,
:app => app
:app => app,
:klass => self
})
end
end
Expand All @@ -328,6 +335,7 @@ def route(methods, paths = ['/'], route_options = {}, &block)
endpoint_options = {
:method => methods,
:path => paths,
:klass => self,
:route_options => (@namespace_description || {}).deep_merge(@last_description || {}).deep_merge(route_options || {})
}
endpoints << Grape::Endpoint.new(settings.clone, endpoint_options, &block)
Expand Down
16 changes: 15 additions & 1 deletion lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def initialize(settings, options = {}, &block)
@block = self.class.generate_api_method(method_name, &block)
end
@options = options
@klass = options[:klass]

raise Grape::Exceptions::MissingOption.new(:path) unless options.key?(:path)
options[:path] = Array(options[:path])
Expand Down Expand Up @@ -358,12 +359,25 @@ def endpoints
end
end

def helper
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be helpers, because it represents several helpers. When you stub, you stub one of the helpers and it's strange to do .helper.stub(:method), semantically.

@klass.helper if @klass
end

def method_missing(method_name, *args, &block)
if helper.respond_to?(method_name)
helper.set_context(params, request, env)
helper.send(method_name, *args, &block)
else
super
end
end

def run(env)
@env = env
@header = {}
@request = Grape::Request.new(@env)

self.extend helpers
#self.extend helpers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't keep comments around, delete.

cookies.read(@request)

run_filters befores
Expand Down
20 changes: 20 additions & 0 deletions lib/grape/helper_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Grape
class HelperObject

def set_context(params, request, env)
reset_context
@params = params
@request = request
@env = env
end

def reset_context
instance_variables.each do |name|
next if name.to_s == "@mock_proxy"
instance_variable_set(name, nil)
end
end

attr_reader :params, :request, :env
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: I am not in love with the name. Maybe HelperContext? In which case we could also change set_context to set and reset_context to reset! (notice my bang notations, I wonder whether that would be more readable.

2 changes: 1 addition & 1 deletion lib/grape/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Grape
VERSION = '0.4.2'
VERSION = '0.4.3'
end
20 changes: 20 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,26 @@ def secret
last_response.body.should eql 'always there:only in admin'
end

it "allows stubbing helpers" do
subject.helpers do
def foo
'foo'
end
end

subject.get "/" do
foo
end

get "/"
last_response.body.should eql "foo"

subject.helper.stub!(:foo).and_return("bar")

get "/"
last_response.body.should eql "bar"
end

it 'is reopenable' do
subject.helpers do
def one
Expand Down