Forum: Ruby on Rails user engine: Role.count() is zero in tests

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ef0db53920b243d6758c2f6b1306df0d?d=identicon&s=25 Steve Ross (cwd)
on 2005-12-17 01:20
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
05d703f649ef1d07e78d7b479fb4c4ac?d=identicon&s=25 James Adam (Guest)
on 2005-12-17 01:47
(Received via mailing list)
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
Ef0db53920b243d6758c2f6b1306df0d?d=identicon&s=25 Steve Ross (cwd)
on 2005-12-17 02:08
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?
05d703f649ef1d07e78d7b479fb4c4ac?d=identicon&s=25 James Adam (Guest)
on 2005-12-17 02:56
(Received via mailing list)
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
Ef0db53920b243d6758c2f6b1306df0d?d=identicon&s=25 Steve Ross (cwd)
on 2005-12-17 07:14
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)
This topic is locked and can not be replied to.