Spec a before_destroy callback in Sequel


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 }

 # ...


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

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

     [@migration_1, @migration_2])




So I’ve committed these sins:

  • redefined someone else’s code, when I depend on the real version at
  • 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.



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