# User engine: Role.count() is zero in tests

**URL:** <https://www.ruby-forum.com/t/user-engine-role-count-is-zero-in-tests/50496>\
**Category:** Rails\
**Created:** [December 17, 2005, 1:20am UTC](https://www.ruby-forum.com/t/user-engine-role-count-is-zero-in-tests/50496 "2005-12-17T01:20:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [December 17, 2005, 1:20am UTC](https://www.ruby-forum.com/t/user-engine-role-count-is-zero-in-tests/50496/1 "2005-12-17T01:20:35Z")

</div>

Subject pretty much says it all. When user\_engine#check\_system\_roles is  
called, it discovers that Role.count is zero. However using  
script/console to execute the same query reveals three users.

N.b., this does not happen in the actual application; only when running  
tests.

Any help is appreciated.

Thx

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [December 17, 2005, 1:47am UTC](https://www.ruby-forum.com/t/user-engine-role-count-is-zero-in-tests/50496/2 "2005-12-17T01:47:02Z")

</div>

Can you confirm there are roles in your test database? I.e. are you  
using the console to connect to the test DB or the development DB  
(default) ?

- james

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [December 17, 2005, 2:08am UTC](https://www.ruby-forum.com/t/user-engine-role-count-is-zero-in-tests/50496/3 "2005-12-17T02:08:17Z")

</div>

I think you’re on the right track here. Testing is hard on the test  
databases. These are the user engine tests – unmodified, but they seem  
to clean out the entire users, users\_roles, and roles tables as a side  
effect.

The problem may be occurring the second time the model is instantiated.  
Note: I did connect to the development database for my script/console  
tests, but I absolutely cloned it to the test database. Again, not that  
cloning helps, cuz the test process for some reason removes all records  
in the test database.

I know test databases are meant to be discardable, but this set of tests  
appears to rely on at least some initial state for the database.

Suggestions?

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [December 17, 2005, 2:56am UTC](https://www.ruby-forum.com/t/user-engine-role-count-is-zero-in-tests/50496/4 "2005-12-17T02:56:10Z")

</div>

Try commenting out the line in the test\_helper.rb file of the user  
engine which loads the schema in - this seems to be causing some  
issues in some cases… If the schema isn’t being loaded at the start  
of each test run, your data shouldn’t get wiped out. Then again, it  
should be being loaded from fixtures anyway…

- james

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [December 17, 2005, 7:14am UTC](https://www.ruby-forum.com/t/user-engine-role-count-is-zero-in-tests/50496/5 "2005-12-17T07:14:01Z")

</div>

I tried…note the commented out line between the logger.debug  
statements. Oddly, the debug statements don’t find their way into  
test.log. Hmmmm. Thoughts?

# Load the LoginEngine schema & mocks

load(File.join(Engines.get(:login).root, “db”, “schema.rb”))  
require File.join(Engines.get(:login).root, ‘test’, ‘mocks’, ‘time’)  
require File.join(Engines.get(:login).root, ‘test’, ‘mocks’, ‘mail’)

# Load the schema - if migrations have been performed, this will be up

to date.  
logger.debug “\*\*\*\*\*\*\*\*\* before schema load there are #{Role.count}  
roles”  
#load(File.dirname( **FILE** ) + “/…/db/schema.rb”)  
logger.debug “\*\*\*\*\*\*\*\*\* after schema load there are #{Role.count} roles”

# set up the fixtures location

Test::Unit::TestCase.fixture\_path = File.dirname( **FILE** ) +  
“/fixtures/”  
$LOAD\_PATH.unshift(Test::Unit::TestCase.fixture\_path)
