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

aslak hellesoy aslak.hellesoy at gmail.com
Mon May 28 09:11:27 EDT 2007


On 5/28/07, Luis Lavena <luislavena at gmail.com> wrote:
> 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).
>

O don't have an answer to what's wrong, but I'm wondering what you're
trying to achieve by expecting certain objects to respond to certain
methods. Why aren't you just invoking these methods instead?

> Ruby: 1.8.5 mswin32
> OS: Windows XP SP2 (English)
> RSpec: 0.9.4 (upgrade to 1.0.3 fails with "NoMethodError" 'fixtures').

What command fails with NoMethodError and what's the full backtrace?
And how can we reproduce it? I'd rather help people upgrade to 1.0
than supporting older versions.

Aslak

> 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
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
>


More information about the rspec-users mailing list