Luis Lavena
2007-May-28 10:55 UTC
[rspec-users] Rails, respond_to? over anonymous module (extend has_many).
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: rubyforge.org/pipermail/rspec-users/attachments/20070528/3c983f56/attachment.txt
aslak hellesoy
2007-May-28 13:11 UTC
[rspec-users] Rails, respond_to? over anonymous module (extend has_many).
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 > rubyforge.org/mailman/listinfo/rspec-users > >
Luis Lavena
2007-May-28 16:30 UTC
[rspec-users] Rails, respond_to? over anonymous module (extend has_many).
On 5/28/07, aslak hellesoy <aslak.hellesoy at gmail.com> wrote: [...]> > 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? >Good point, old habit :-P> > 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. >Will get you the backtrace later, when return to the office, but is related to fixtures: describe User do fixtures :users ... end When running, it report that DSL / Rails / EvalContext don''t have any method defined "fixtures" (from top of my head, will get you specific data later today). Thanks for your time. -- 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
Maybe Matching Threads
- ActiveRecord::AssociationTypeMismatch
- Association extension method
- shouldn''t this work? - session[:array_of_objects].delete_if {|x| x.id == params[:id]}
- Overwriting / Decorating ActiveRecord association accessor
- [LLVMdev] Compiling issues: undefined reference to `.Lline_table_start1'