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

Conversation

unorthodoxgeek
Copy link

moved the helpers to a helper object held in the API class, which allows it to get stubbed and mocked for testing purposes.

@unorthodoxgeek
Copy link
Author

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.

@unorthodoxgeek
Copy link
Author

closing until I can get specs to pass.

@unorthodoxgeek
Copy link
Author

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.
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.

@dblock
Copy link
Member

dblock commented Apr 30, 2013

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.

@dblock
Copy link
Member

dblock commented Apr 30, 2013

This fixes #396 and is an alternative for #397.

@@ -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.

@unorthodoxgeek
Copy link
Author

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.

@mbleigh
Copy link
Contributor

mbleigh commented Apr 30, 2013

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

@unorthodoxgeek
Copy link
Author

any suggestions as to how we should fix this? I'm thinking about a hash... would appreciate your feedback

@dblock
Copy link
Member

dblock commented May 2, 2013

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 setup did which is accessible externally and therefore can have stubbed methods?

dblock added a commit to dblock/grape that referenced this pull request May 26, 2013
@dblock
Copy link
Member

dblock commented May 26, 2013

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 stub and stub! ourselves on the helpers module being generated and executing stubs at runtime?

@guilherme
Copy link

i'm having this issue. how about the conclusion of the pull request?

@dblock
Copy link
Member

dblock commented Oct 18, 2013

@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.

@zuk
Copy link

zuk commented Jan 8, 2014

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

@dblock
Copy link
Member

dblock commented Jul 6, 2014

I am going to close this PR as it doesn't address the issue (see above).

@dblock dblock closed this Jul 6, 2014
@coveralls
Copy link

Coverage Status

Changes Unknown when pulling a13c729 on unorthodoxgeek:master into * on intridea:master*.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants