I’m trying to extract a set of models from a Rails 3.2.3 app into a gem so they can be used as a shared interface between applications.
I moved the model into a module and put it in lib/invite_interface/invite.rb
module InviteInterface
class Invite < ActiveRecord::Base
belongs_to :user
end
def to_json; end;
def from_json; end;
end
I plopped rspec into the gemfile, got it running successfully, created the following spec:
require 'spec_helper'
describe InviteInterface::EncounterSurvey do
it 'should belong to user' do
subject.should respond_to(:user)
end
end
Unfortunately, I can’t execute rspec on the model, because active record/rspec wants an active connection.
1) InviteInterface::Invite should belong to encounter survey set
Failure/Error: subject.should respond_to(:user)
ActiveRecord::ConnectionNotEstablished:
ActiveRecord::ConnectionNotEstablished
# /Users/justin/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:398:in `retrieve_connection'
# /Users/justin/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:168:in `retrieve_connection'
# /Users/justin/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:142:in `connection'
# /Users/justin/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/activerecord-3.2.3/lib/active_record/model_schema.rb:228:in `columns'
# /Users/justin/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/activerecord-3.2.3/lib/active_record/model_schema.rb:243:in `column_defaults'
How do I prevent ActiveRecord from looking for a DB connection?
You need to test your library with a database anyway, so you might as well use an in-memory SQLite database for testing. Just add this to
spec_helper.rb:and create your schema as follows: