Spec a before_destroy callback in Sequel

Hi

I’m struggling to find a neat way to spec this. The Sequel ORM
doesn’t support automatically destroying dependent models. It’s easy
enough to do in a callback though:

class Changeset < Sequel::Model
has_many :migrations, :class => DataModel::Migration

 before_destroy do
   migrations.each { |m| m.destroy }
 end

 # ...

end

The problem is isolating it from the database to test. I want to know
that when I send Changeset#destroy that each migration is destroyed.
The implementation of Sequel::Model#destroy (from the RDoc) is this:

File sequel/lib/sequel_model/record.rb, line 325

325: def destroy
326: db.transaction do
327: before_destroy
328: delete
329: after_destroy
330: end
331: self
332: end

Now, if a database isn’t wired up, Sequel complains if you do anything
database related, and that includes using associations. So the best I
have so far is this:

describe “#destroy” do
before(:each) do
# …
@changeset = DataModel::Changeset.new
end

 it "should fire a before_destroy hook to destroy all Migrations" do
   class << @changeset
     def destroy
       before_destroy
     end
   end

   @changeset.stub!(:migrations).and_return(
     [@migration_1, @migration_2])

   @migration_1.should_receive(:destroy)
   @migration_2.should_receive(:destroy)

   @changeset.destroy
 end

end

So I’ve committed these sins:

  • redefined someone else’s code, when I depend on the real version at
    runtime
  • included an assumption about the external code in my stub
  • stubbed out a method on the object under test

In sort, it’s suckitude of the highest order. But I don’t know how to
do this without resorting to an in-memory SQLite database or something.

Any help much appreciated.

Ashley


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