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:inquote’
    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:inquote_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:infind_from_ids’
    whenever/gems/activerecord-2.3.8/lib/active_record/base.rb:619:in
    find' lib/generic_message_functions.rb:4:indelegate_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:insend
    /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

On Jul 12, 7:28 am, martins [email protected] 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