Commit
- Loading branch information
There are no files selected for viewing
4 comments
on commit 34ae840
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.
Why not just use the framework to manage all of this? Something like:
def any_instance stub(:new) do # ... end end
That way the framework will restore the new
method at the end of the example using already existing machinery.
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.
If I'm understanding this right, we'd need to be able to create a new functioning instance using every time new
is invoked, so you'd have to be able to do something like
def any_instance
stub(:new) do |*args, &block|
# invoke original unstubbed new with *args, &block
# stub resulting instance using messages recorded by the recorder
end
__recorder
end
Wouldn't this also fail on 1.8.7 in cases where new
takes a block?
Is there anything I'm missing? (and my apologies for being this slow on the uptake)
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.
Take a look at http://github.com/rspec/rspec-mocks/commit/62e1abfe613ff33b53c771be3cdc651545047d85
That's just a spike to demonstrate - there are no specs to show new() gets reset, but you get the idea.
Also - I noticed that this approach doesn't work with String.any_instance
, for example. I'm not sure how mocha handles this, but it does work with String
so you might want to take a peek at that.
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.
So it looks like "", [], {} and class bypass String.new, Array.new, Hash.new and Class.new respectively for performance reasons and the parser of course doesn't expose any hooks for us to latch onto except in rbx. I knew this about Class.new but it didn't strike me until you pointed out that doing String.any_instance doesn't affect strings created with "".
Seems like a deal breaker - I'll have to rethink the implementation so that it doesn't hinge on new. I've got some ideas from mocha, so I'll take another shot at it.
I'm not sure why using #methods.include? instead of #respond_to? fixes the "restores the class to its original state after each example" spec, but only on 1.9.2 - it still fails on 1.8.7.