[rspec-users] Rails, respond_to? over anonymous module (extend has_many).

Luis Lavena luislavena at gmail.com
Mon May 28 06:55:25 EDT 2007


Hello List,

I'm trying to generate examples for some list-helpers I have coded
which use in my projects.

Basically, the Playlist class uses one anonymous module in has_many
that acts as helper between acts_as_list and my desired API:

class Playlist < ActiveRecord::Base
  # associations go here
  has_many :playlist_items, :order => :position, :dependent => :destroy
  has_many :assets, :through => :playlist_items, :order => :position do
    def << (*assets)
      assets.flatten.each do |asset|
        item = proxy_owner.playlist_items.build(:asset => asset)
        item.save unless proxy_owner.new_record?
        proxy_owner.duration += asset.duration
      end
      proxy_owner.save unless proxy_owner.new_record?
    end

    def delete_at (position)
      pli = proxy_owner.playlist_items.find_by_position(position) rescue nil
      unless pli.nil?
        duration = pli.asset.duration
        proxy_owner.playlist_items.delete(pli)
        proxy_owner.update_attributes( :duration =>
(proxy_owner.duration - duration) )
      end
    end

    def order
      return [] if proxy_owner.new_record?
      proxy_owner.playlist_item_ids
    end

    def reorder (ids)
      return [] if proxy_owner.new_record?
      ids.each_with_index do |id, position|
        PlaylistItem.update(id, :position => position + 1)
      end
      proxy_owner.playlist_items(true)
    end
  end

  # validations goes here
  validates_presence_of :title
  validates_uniqueness_of :title

  # code after this point
end

==

Now, running a simple spec like this:

describe Playlist do
  fixtures :playlists

  before(:each) do
    @list = Playlist.new
  end

  it { @list.assets.should respond_to(:<<) }
  it { @list.assets.should respond_to(:delete_at) }
  it { @list.assets.should respond_to(:order) }
  it { @list.assets.should respond_to(:reorder) }
end

==

Only #order and #reorder report errors:

1)
'Playlist should respond to #order' FAILED
expected target to respond to :order
./spec\models\playlist_spec.rb:16:

2)
'Playlist should respond to #reorder' FAILED
expected target to respond to :reorder
./spec\models\playlist_spec.rb:17:

Attached is the backtrace (--backtrace) when running with spec command.

Any hint?
Command works and do what its expected, but respond_to from RSpec
don't see them (from console they work).

Ruby: 1.8.5 mswin32
OS: Windows XP SP2 (English)
RSpec: 0.9.4 (upgrade to 1.0.3 fails with "NoMethodError" 'fixtures').
Rails: 6703 (Edge, pistoned)

Thanks in advance guys!

-- 
Luis Lavena
Multimedia systems
-
Leaders are made, they are not born. They are made by hard effort,
which is the price which all of us must pay to achieve any goal that
is worthwhile.
Vince Lombardi
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: backtrace.txt
Url: http://rubyforge.org/pipermail/rspec-users/attachments/20070528/3c983f56/attachment.txt 


More information about the rspec-users mailing list