[rspec-users] Name collision - how would you handle this?

David Chelimsky dchelimsky at gmail.com
Sat Aug 7 18:18:13 EDT 2010

On Aug 7, 2010, at 4:37 PM, Ashley Moran wrote:

> On 7 Aug 2010, at 22:10, David Chelimsky wrote:
>> So - what should we do? I don't think changing Minitest is really an option, as too many assertion libraries already wrap Minitest assertions. I don't think RSpec should be in the business of monitoring methods end-users define to make sure they're not overriding pre-existing methods (what if you override a method intentionally?). The only thing I'm left with is document this particular case and hope for the best, but that feels unsatisfactory as well.
> While I fully agree if you `def` a method that already exists, you should be expected to deal with it yourself (that's just the way things are in Ruby), does the same apply to `let`?  I can actually see an argument that you should only be able to `let` a method that doesn't already exist, and also only do it once (which is just a consequence of not being able to override a method, given the current implementation). 

> Can you think of any downsides of preventing RSpec users from overriding existing methods with `let`?

Yes. Let's say I write a shared example group with this:

def foo
  raise "you need to define a foo method in the block passed to it_should_behave_like"

And you override it using let(:foo), which would be a perfectly reasonable way to handle it. In fact, it would be the way I would handle in instinctively, because now I don't have to wrote my own memoization handling into the method.

> Are there any popular names already taken?  Or other problems?
> To me, `let` is magic.  I don't think of it, first and foremost, of defining a method.

From the RDoc: Generates a method whose return value is memoized after the first call.

> I see the things it creates as more like local variables, and just remind myself that they're methods if I wonder why it works.
> I'm not sold either way on this, but I think it's one worth a debate.

The debate is on! Any other opinions out there?

> Ash

More information about the rspec-users mailing list