Spec'ing calls to super (or other Ruby keywords)

Hi there,

How does one spec an invocation of a Ruby keyword, such as super in this
case?

class User < ActiveResource::Base

faking the ActiveRecord before/after_save observers

def save
super
UserMailer.deliver_activation(self) if recently_activated?
end
end

Does the solution look anything like the following?

describe User do
describe ‘#save’ do
it “should call save on the parent class” do
# something.should_receive(:something)
@user.save
end
end
end

Any thoughts?

Thanks much,
Matt


View this message in context:
http://www.nabble.com/spec'ing-calls-to-super-(or-other-Ruby-keywords)-tp17027929p17027929.html
Sent from the rspec-users mailing list archive at Nabble.com.

I believe calls to super are sufficiently internal to the Ruby
interpreter
that a mocking framework can’t intercept them without doing separate
implementations for separate Ruby interpreters (and likely even separate
versions). I could be wrong, but even so I’d recommend a different
approach.

If your need is really as simple as your example, what you have is just
a
method that has to get two things done: the base save and one additional
call. You can write one (or more) example for each of those two things
without your spec knowing that one of those things gets done by calling
super. (You might object that by spec’ing the base #save behavior
you’re
spec’ing the framework. I’d say you’re USING the framework to spec
something your code does. To be clear, I’m not suggesting you spec
every
detail of what save does: just something to make sure the record
actually
lands in the db.)

(Sidebar: Keep in mind the return value if you’re really overriding
#save
like that.)

If you’re dead set on spec’ing that the super method gets called, there
are
a couple of hideous ways of doing it that will leak out of your example.
Namely, you can (in your spec) redefine the method in the superclass and
verify it gets called or (also in your spec, and this one’s a little
less
leaky) have the class under test include a module that defines the same
method and verify it gets called. Don’t do either of those though
(unless
it’s just to prove to yourself that they’re possible).

-hume.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs