[rspec-users] Can I use mocking in this way?

steve ross cwdinfo at gmail.com
Mon Apr 5 22:37:22 EDT 2010


On Apr 5, 2010, at 8:09 AM, Pat Maddox wrote:
> 
> Sounds like a lot of work
> 
> 
> On Apr 3, 2010, at 9:08 PM, Julian Leviston wrote:
> 
>> Sorry I meant send AND __send__
>> 
>> Julian.
>> 
>> On 04/04/2010, at 11:45 AM, Julian Leviston wrote:
>> 
>>> 
>>> On 04/04/2010, at 7:32 AM, David Chelimsky wrote:
>>> 
>>>> On Sat, Apr 3, 2010 at 8:56 AM, Vojto Rinik <zero0xxx at gmail.com> wrote:
>>>>> Hello RSpec users!
>>>>> I have one abstract class and a few classes that inherit from that abstract
>>>>> one.
>>>> 
>>>> Ruby doesn't have abstract classes. You can have a base class that you
>>>> don't _intend_ to instantiate directly, but there's nothing stopping
>>>> you from doing so, so it's not like an abstract class in java, which
>>>> you actually can't instantiate directly.
>>>> 
>>>> I've seen some cases where the initialize method is made private so
>>>> you can't just call Foo.new, so it sort of feels like an abstract
>>>> class, but even in that case you can still use send() to instantiate
>>>> one in a test:
>>>> 
>>>> AbstractIshClass.send(:new)
>>>> 
>>> 
>>> How about if you overrode new and __new__ ?
>>> 
>>> Julian.

How about:

class AbstractClassException < RuntimeError; end
class AbstractishClass
  def initialize
    raise AbstractClassException.new("don't do that.")
  end
end

That way, even sending a message to new causes a failure. And that's what you're looking for, right?


More information about the rspec-users mailing list