martins
2010-Jul-12 06:28 UTC
User.find() unavailable for module when accessed through an test
Hi, The methods I´ve put in GenericMessageFunctions are available and working within the Rails app, but I can´t access them from my tests. Why isn´t User.find() available from the GenericMessageFunctions module when it´s accessed trough a test? Should I include some library to get it working? $ ruby test/unit/tweet_test.rb 1) Error: test_redelegating_ownership(TweetTest): TypeError: can''t dump anonymous class Class /opt/local/lib/ruby/1.8/yaml/rubytypes.rb:6:in `to_yaml'' whenever/gems/activerecord-2.3.8/lib/active_record/ connection_adapters/abstract/quoting.rb:31:in `quote'' whenever/gems/activerecord-2.3.8/lib/active_record/ connection_adapters/mysql_adapter.rb:236:in `quote'' whenever/gems/activerecord-2.3.8/lib/active_record/base.rb:1443:in `quote_value'' whenever/gems/activerecord-2.3.8/lib/active_record/base.rb:1608:in `find_one'' whenever/gems/activerecord-2.3.8/lib/active_record/base.rb:1599:in `find_from_ids'' whenever/gems/activerecord-2.3.8/lib/active_record/base.rb:619:in `find'' lib/generic_message_functions.rb:4:in `delegate_to'' test/unit/tweet_test.rb:62:in `test_redelegating_ownership'' /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/ active_support/testing/setup_and_teardown.rb:62:in `__send__'' /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/ active_support/testing/setup_and_teardown.rb:62:in `run'' 1 module GenericMessageFunctions 2 3 def delegate_to(user_id, *msg_note) 4 new_user = User.find(user_id) 5 self.delegated_to = user_id 6 self.save 7 end 8 9 end ## tweet_test test "redelegating ownership" do tweet = Tweet.make(:user_id => 63) user = User.make(:id => 83) tweet.delegate_to(User) assert(tweet.delegated_to, 83) end ## Tweet.rb require ''generic_message_functions.rb'' include GenericMessageFunctions class Tweet < ActiveRecord::Base end -- Best, Martin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Jul-12 06:59 UTC
Re: User.find() unavailable for module when accessed through an test
On Jul 12, 7:28 am, martins <mar...-sCXqIeMX0Gf7tLni4+Tb9Q@public.gmane.org> wrote:> Hi, > > The methods I´ve put in GenericMessageFunctions are available and > working within the Rails app, but I can´t access them from my tests. > Why isn´t User.find() available from the GenericMessageFunctions > module when it´s accessed trough a test? Should I include some library > to get it working? >It''s not that it''s not available but it''s blowing up because you''re passing the class User to delegate_to when presumably you meant to pass the instance you created a few lines previously Fred> $ ruby test/unit/tweet_test.rb > > 1) Error: > test_redelegating_ownership(TweetTest): > TypeError: can''t dump anonymous class Class > /opt/local/lib/ruby/1.8/yaml/rubytypes.rb:6:in `to_yaml'' > whenever/gems/activerecord-2.3.8/lib/active_record/ > connection_adapters/abstract/quoting.rb:31:in `quote'' > whenever/gems/activerecord-2.3.8/lib/active_record/ > connection_adapters/mysql_adapter.rb:236:in `quote'' > whenever/gems/activerecord-2.3.8/lib/active_record/base.rb:1443:in > `quote_value'' > whenever/gems/activerecord-2.3.8/lib/active_record/base.rb:1608:in > `find_one'' > whenever/gems/activerecord-2.3.8/lib/active_record/base.rb:1599:in > `find_from_ids'' > whenever/gems/activerecord-2.3.8/lib/active_record/base.rb:619:in > `find'' > lib/generic_message_functions.rb:4:in `delegate_to'' > test/unit/tweet_test.rb:62:in `test_redelegating_ownership'' > /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/ > active_support/testing/setup_and_teardown.rb:62:in `__send__'' > /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/ > active_support/testing/setup_and_teardown.rb:62:in `run'' > > 1 module GenericMessageFunctions > 2 > 3 def delegate_to(user_id, *msg_note) > 4 new_user = User.find(user_id) > 5 self.delegated_to = user_id > 6 self.save > 7 end > 8 > 9 end > > ## tweet_test > > test "redelegating ownership" do > tweet = Tweet.make(:user_id => 63) > user = User.make(:id => 83) > > tweet.delegate_to(User) > assert(tweet.delegated_to, 83) > end > > ## Tweet.rb > > require ''generic_message_functions.rb'' > include GenericMessageFunctions > > class Tweet < ActiveRecord::Base > end > > -- > Best, > Martin-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.