Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing ActiveRecord models inside a gem?

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?

like image 943
Allyl Isocyanate Avatar asked May 15 '12 16:05

Allyl Isocyanate


People also ask

Is ActiveRecord a gem?

ActiveRecord is a gem that is part of Ruby on Rails. It is the ORM, i.e. the library that maps our objects to tables. In other words, it is the Ruby library that allows us to use Ruby classes in order to access our data stored in an RDBMS, like MySQL or PostgreSQL.

What's the purpose of ActiveRecord?

Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database. It is an implementation of the Active Record pattern which itself is a description of an Object Relational Mapping system.

What is ORM in ror?

ORM is Object Relational Mapper. It means you don't have to manually call the database yourself; the ORM handles it for you. Ruby on Rails uses one called ActiveRecord, and it's a really good one. ORM allows you to do things such as: User.

What is rails ActiveRecord in ruby on rails?

Rails Active Records provide an interface and binding between the tables in a relational database and the Ruby program code that manipulates database records. Ruby method names are automatically generated from the field names of database tables.


1 Answers

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:

ActiveRecord::Base.establish_connection(:adapter => "sqlite3", 
                                       :database => ":memory:")

and create your schema as follows:

ActiveRecord::Schema.define do
  self.verbose = false

  create_table :invites, :force => true do |t|
    t.string :text
  end
  ...
end
like image 145
Yuri Barbashov Avatar answered Sep 17 '22 02:09

Yuri Barbashov