Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Create temporary table-backed ActiveRecord models for use in tests

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Gemfile.lock
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 temping.gemspec
README.md

Temping

Code Climate Build Status Gem Version

Description

Temping allows you to create arbitrary ActiveRecord models backed by a temporary SQL table for use in tests. You may need to do something like this if you're testing a module that is meant to be mixed into ActiveReord models without relaying on a concrete class.

Temping will use your existing database connection. As we're using temporary tables all data will be dropped when the database connection is terminated.

Examples

The basic setup of a model involves calling create with a symbol that represents the class name of the model you wish to create. By default, this will create a temporary table with an id column.

Temping.create :dog

Dog.create => #<Dog id: 1>
Dog.table_name => "dogs"
Dog => Dog(id: integer)

Additional database columns can be specified via the with_columns method which uses Rails migration syntax:

Temping.create :dog do
  with_columns do |t|
    t.string :name
    t.integer :age, :weight
  end
end

Dog.create 

# => #<Dog id: 1, name: nil, age: nil, weight: nil>

When a block is passed to create, it is evaluated in the context of the class. This means anything you do in an ActiveRecord model class body can be accomplished in the block including method definitions, validations, module includes, etc.

Temping.create :dog do
  validates :name, presence: true

  with_columns do |t|
    t.string :name
    t.integer :age, :weight
  end

  def quack
    "arf!"
  end
end

Dog.create!

# => ActiveRecord::RecordInvalid: Validation failed: Name can't be blank

codey = Dog.create! name: "Codey"
codey.quack

# => "arf!"

Installation

In your Gemfile:

gem "temping"

Bugs, Features, Feedback

Tickets can be submitted by via GitHub issues.

Something went wrong with that request. Please try again.