I have a model structure like this
models/
-- a.rb
-- b.rb
-- project_a/
---- a.rb
---- b.rb
the classes inside the folder project_a
are classes from another Rails project. I don't have the migrations for them, I just use them to call find
. It is a project to migrate data. Actually, all these classes from project_a
, inhert this class:
class ProjectA::ProjectADatabase < ActiveRecord::Base
self.abstract_class = true
establish_connection "project_a_#{Rails.env == 'production' ? 'production' : 'development' }".to_sym
end
So, as I don't have migrations, I can't use factories to test them, so, I'm doubling. Most of my doubles are like this
def stubs_all_finds
allow(ProjectA::Like).to receive(:find).and_return(create_like)
end
def create_like
like = double(ProjectA::Like)
end
but when I push to Github, codeship fails to run the tests:
1) Class validations
Failure/Error: stubs_all_finds
ActiveRecord::AdapterNotSpecified:
'project_a_development' database is not configured. Available: ["development", "test"]
Seems like that even doubled, my class is trying to connect to the database. Is there a way to prevent it?
Your classes are trying to connect to the database as soon as they are defined - the mere act of requiring that base class will attempt a database connection, because the class definition contains the call to
establish_connection
You could skip the call to
establish_connection
when in tests - if you're stubbing out all subsequent database access then it shouldn't matter that the base class is pointing at the wrong database.Alternatively you might consider creating a project_a_test_database - you could copy the schema.rb from the other application and load that into a separate database.