# User.find() unavailable for module when accessed through an test

**URL:** <https://www.ruby-forum.com/t/user-find-unavailable-for-module-when-accessed-through-an-test/191410>\
**Category:** Rails\
**Created:** [July 12, 2010, 8:29am UTC](https://www.ruby-forum.com/t/user-find-unavailable-for-module-when-accessed-through-an-test/191410 "2010-07-12T08:29:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![martins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@martins](https://www.ruby-forum.com/u/martins)\
**Post date:** [July 12, 2010, 8:29am UTC](https://www.ruby-forum.com/t/user-find-unavailable-for-module-when-accessed-through-an-test/191410/1 "2010-07-12T08:29:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![martins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@martins](https://www.ruby-forum.com/u/martins)\
**Post date:** [July 12, 2010, 9:00am UTC](https://www.ruby-forum.com/t/user-find-unavailable-for-module-when-accessed-through-an-test/191410/2 "2010-07-12T09:00:58Z")

</div>

On Jul 12, 7:28 am, martins [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
