Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Log a warning when methods are reset on a frozen object #496

Closed
wants to merge 1 commit into from

Conversation

jcoyne
Copy link
Contributor

@jcoyne jcoyne commented Dec 19, 2013

No description provided.

@@ -85,6 +85,9 @@ def restore_original_method
restore_original_visibility

@method_is_proxied = false
rescue => e
Copy link
Member

Choose a reason for hiding this comment

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

We should rescue the specific exception here? It'll be at least a RunTimeError...

@JonRowe
Copy link
Member

JonRowe commented Dec 19, 2013

Thanks for tackling this :)

context "on a frozen object" do
it "removes the method double" do
target.to receive(:foo).and_return(:baz)
expect_warning_without_call_site(/Unable to remove stub method foo because the object was frozen./).at_least(:once)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The test passes, but the warning is still issued to $stdout. Is that a bug in rspec-support?

Copy link
Member

Choose a reason for hiding this comment

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

I don't see how that's possible... o_O

Copy link
Member

Choose a reason for hiding this comment

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

Also the at_least(:once) call should be unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My initial pass didn't have at_least(:once), but then it fails:

     Failure/Error: expect_warning_without_call_site(/Unable to remove stub method foo because the object was frozen./)
       (Kernel).warn(any args)
           expected: 1 time with any arguments
           received: 2 times with any arguments

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here's what the output looks like showing the messages:

All examples were filtered out; ignoring {:focus=>true}
..................................................................................................................................................................................................................................................................................................................................*.................*.............................................................................................................................................................................................................................................................................................*.*.......................Unable to remove stub method foo because the object was frozen.
.............Unable to remove stub method foo because the object was frozen.
......................................................*.......................................................................................................................................................................................................................................................................................................................................................................

Copy link
Member

Choose a reason for hiding this comment

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

It could of course be happening 3 times...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like I can remove the at_least(:once) if I also remove the reset object, but then the test becomes less clear. Alternately, I can change the order of the expectations are declared, and then the at_least(:once) isn't necessary.

Copy link
Member

Choose a reason for hiding this comment

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

clean up the mock manually so it doesn't need to be cleaned up outside the test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But we can't because it's frozen, right?

Copy link
Member

Choose a reason for hiding this comment

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

I opened a PR to supress it jcoyne#1

@jcoyne
Copy link
Contributor Author

jcoyne commented Dec 19, 2013

Thanks for your help @JonRowe. I squished your PR into this one.

@@ -85,6 +86,15 @@ def restore_original_method
restore_original_visibility

@method_is_proxied = false
rescue RuntimeError => e
Copy link
Member

Choose a reason for hiding this comment

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

Ah ok, this needs to be TypeError on 1.8.7...

Copy link
Member

Choose a reason for hiding this comment

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

I'm now wondering if it would be better to rescue StandardError and inspect the message for the word frozen... Thoughts @myronmarston, am I being too picky?

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 checking frozen? on the singleton class (as it is now) is a less brittle approach.

Copy link
Member

Choose a reason for hiding this comment

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

Just drop back to a bare rescue?

Copy link
Member

Choose a reason for hiding this comment

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

Yep, that sounds fine.

One thing thats unclear to me: what purpose does @show_frozen_warnings serve? I don't see any spec showing what it does...

Copy link
Member

Choose a reason for hiding this comment

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

Prevents the message appearing twice for the same method. We're still reseting mocks twice.

@fables-tales
Copy link
Member

Closing in favour of #527

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

Successfully merging this pull request may close these issues.

4 participants