-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Conversation
seems like some versions of Ruby have different ways of stubbing, tests fail on 1.8.7 and ree. Rubinius just takes forever to run. |
closing until I can get specs to pass. |
should now pass |
@@ -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. |
There was a problem hiding this comment.
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 therequire
, just to get exactly to the point of how to stub something.
I think this is really good, cc: @mbleigh - I definitely think this is a better implementation, and I don't see any immediate holes. Would love you to check this out in detail. @unorthodoxgeek: please also update CHANGELOG. |
@@ -358,12 +359,25 @@ def endpoints | |||
end | |||
end | |||
|
|||
def helper |
There was a problem hiding this comment.
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.
applied changes, the helper method on api can't be named helpers as that method name is already taken. I've changed it to a more descriptive name "helpers_container", feel free to suggest a different name. |
The problem comes in when you have multiple levels of helper methods. Failing spec example: it "allows stubbing nested helpers" do
subject.helpers do
def foo
'foo'
end
end
subject.get "/" do
foo
end
subject.namespace :random do
helpers do
def foo
'bar'
end
end
get :what do
foo
end
end
get '/random/what'
last_response.body.should == 'bar'
get '/'
last_response.body.should == 'foo'
subject.helper.stub!(:foo).and_return("baz")
get '/'
last_response.body.should == 'baz'
get '/random/what'
last_response.body.should == 'baz'
end |
any suggestions as to how we should fix this? I'm thinking about a hash... would appreciate your feedback |
Maybe we can instead combine the two techniques proposed here and in #397, so leaving helpers alone and injecting a class-level (global) helper block the same way |
I combined the test above and what you wrote into https://github.com/dblock/grape/blob/spec-helpers/spec/grape/api_spec.rb#L913 (just copy paste). If we can get both to pass, we win. I think @mbleigh's setup approach might work better, we want to inject whatever is being stubbed at runtime. In a way we want to delay the stubs. Maybe this is something that can be done with a bit more finagling with how RSpec mocks work? Like defining |
i'm having this issue. how about the conclusion of the pull request? |
@guilherme see above, there's no implementation that works correctly. If you make one, please make a PR. You have to make that test pass that's mentioned above. |
Someone put up a $100 bounty to get this resolved: https://www.bountysource.com/issues/655313-can-not-stub-mock-helper-methods/bounties See #396 |
I am going to close this PR as it doesn't address the issue (see above). |
Changes Unknown when pulling a13c729 on unorthodoxgeek:master into * on intridea:master*. |
moved the helpers to a helper object held in the API class, which allows it to get stubbed and mocked for testing purposes.