Best way to determine if RSpec is loaded?

Hi

I’ve been using the Twitter gem, but I discovered it loads
ActiveSupport, which meddles with Kernel#require and generally causes
me confusion and pain. I don’t need Twitter loaded for my specs,
currently I’m doing this:

require ‘twitter’ unless Object.const_defined?(:Spec)

Is there a better way?

This got me thinking… in general, should it matter whether you load
the whole app and dependent libraries for specs? Is a spec any less
isolated if you load the whole app vs just the class the spec relates
to?

The one advantage I know of is if the whole app is available, you can
mock(My::Class) and it’ll tell you if you’re mocking something that
doesn’t exist. That’s good, right?

Thanks

Ashley


http://www.patchspace.co.uk/

On Sun, Sep 28, 2008 at 6:54 PM, Ashley M.
[email protected] wrote:

Hi

I’ve been using the Twitter gem, but I discovered it loads ActiveSupport,
which meddles with Kernel#require and generally causes me confusion and
pain. I don’t need Twitter loaded for my specs, currently I’m doing this:

require ‘twitter’ unless Object.const_defined?(:Spec)

Is there a better way?

Depends on where that is. If it’s in a high level conf file, then it’s
probably the best you can do. If it’s lower down, you could set up
an ENV variable instead and use that as your conditional. Have it
default to true, but have your spec config set it to false. Still
kinda ugly :slight_smile:

This got me thinking… in general, should it matter whether you load the
whole app and dependent libraries for specs? Is a spec any less isolated if
you load the whole app vs just the class the spec relates to?

In theory it should be fine, but when you’re running a suite of
examples you’re going to eventually want to load up everything in the
app, no?

The one advantage I know of is if the whole app is available, you can
mock(My::Class) and it’ll tell you if you’re mocking something that doesn’t
exist. That’s good, right?

RSpec’s mocks don’t do that (tell you when a mocked method doesn’t
really exist). Are you using a mocking frawework that does?

On 29 Sep 2008, at 13:28, David C. wrote:

In theory it should be fine, but when you’re running a suite of
examples you’re going to eventually want to load up everything in the
app, no?

True! But I’ve had a lot of loading issues lately, when autotest runs
a single file and everything breaks.

The one advantage I know of is if the whole app is available, you can
mock(My::Class) and it’ll tell you if you’re mocking something that
doesn’t
exist. That’s good, right?

RSpec’s mocks don’t do that (tell you when a mocked method doesn’t
really exist). Are you using a mocking frawework that does?

Ah, I was referring no the class, not the method. I like being told
My::Class doesn’t exist, it’s a motivation to create the files and write

it “should do something” do
violated
end

I find that helps drive development down the app’s layers.

Ashley


http://www.patchspace.co.uk/