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

Add line numbers to deprecated block in mock constructor #310

Closed
chrisarcand opened this issue Aug 25, 2017 · 2 comments
Closed

Add line numbers to deprecated block in mock constructor #310

chrisarcand opened this issue Aug 25, 2017 · 2 comments

Comments

@chrisarcand
Copy link

chrisarcand commented Aug 25, 2017

After updating to the latest version of mocha, I get many of these deprecations in my test suite:

*** Mocha deprecation warning: Passing a block is deprecated. Use Object#tap or define stubs/expectations with an explicit receiver instead.

This comes from this change: #290

I found it frustrating that the output didn't tell me where in my test suite these deprecated calls are, and didn't really know where to find them to change them appropriately. I suggest that file name and line number information be added.

I did this for myself by making the following change:

diff --git a/lib/mocha/mock.rb b/lib/mocha/mock.rb
index 69e1bcc..5a293cb 100644
--- a/lib/mocha/mock.rb
+++ b/lib/mocha/mock.rb
@@ -261,7 +261,8 @@ module Mocha
       @responder = nil
       @unexpected_invocation = nil
       if block
-        Deprecation.warning('Passing a block is deprecated. Use Object#tap or define stubs/expectations with an explicit receiver instead.')
+        file = caller.detect { |x| !(x.include?('mocha')) }
+        Deprecation.warning("Passing a block is deprecated. Use Object#tap or define stubs/expectations with an explicit receiver instead. Called from #{file}")
         instance_eval(&block)
       end
     end

It works well and looks nice for my use case, but I'm not sure if that simple backtrace check covers all edge cases and sadly don't have time to be more thorough and make a pull request, but I thought I'd share anyway.

@floehopper
Copy link
Member

@chrisarcand: Thanks for reporting this and posting your code changes.

There is an existing un-documented feature whereby you can enable a debug mode which will output backtraces for each deprecation warning. You can do this by setting the MOCHA_OPTIONS environment variable to debug.

I have added a new issue to document MOCHA_OPTIONS and another to improve the deprecation warnings themselves.

Given that there's already a way to get the backtraces for deprecation warnings and that I've captured these other issues, I hope you don't mind but I'm going to close this one. Please let me know if you disagree.

@chrisarcand
Copy link
Author

I hope you don't mind but I'm going to close this one.

Absolutely not, that's great! Thanks for all the info. 👍

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

No branches or pull requests

2 participants