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

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

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

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?

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

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)