0.0
Low commit activity in last 3 years
A long-lived project that still receives updates
You can define a temporary class and use it in ActiveSupport::TestCase.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 7.1
 Project Readme

TemporaryModel

You can define a temporary class and use it in ActiveSupport::TestCase.

Usage

Minitest

class YourTest < ActiveSupport::TestCase
  include TemporaryModel::TestHelper # 1 of 2

  # 2 of 2
  temporary_model 'Post' do
    define_table do |t|
      t.string :title, default: '', null: false
    end

    has_many :taggings, as: :taggable, dependent: :destroy
    has_many :tags, through: :taggings, source: :tag

    validates :title, presence: true

    # You can use custom validators
    validates_with YourCustomValidator

    # You can use Active Storage
    has_one_attached :doc
    has_many_attached :docs 

    def tag_names
      tags.pluck(:name)
    end
  end

  test 'truthy' do
    post = Post.create!(title: 'Hello')
    assert 'Hello', post.title
  end
end

Please check test/*test.rb

RSpec

require 'rails_helper'

RSpec.describe 'TemporaryModelSpec' do
  include TemporaryModel::TestHelper

  temporary_model 'Tag' do
    define_table do |t|
      t.string :name, default: '', null: false
    end

    has_many :taggings, dependent: :destroy
    has_many :posts, through: :taggings, source: :taggable, source_type: 'Post'

    validates :name, presence: true
  end

  temporary_model 'Post' do
    define_table do |t|
      t.string :title, default: '', null: false
    end

    has_many :taggings, as: :taggable, dependent: :destroy
    has_many :tags, through: :taggings, source: :tag

    validates :title, presence: true

    def tag_names
      tags.pluck(:name)
    end
  end

  temporary_model 'Tagging' do
    define_table do |t|
      t.references :tag, foreign_key: true
      t.references :taggable, polymorphic: true
    end

    belongs_to :tag
    belongs_to :taggable, polymorphic: true
  end

  let(:ruby) { Post.create!(title: 'Ruby') }
  let(:rust) { Post.create!(title: 'Rust') }
  
  before do
    ruby.taggings.create!(tag: Tag.create(name: 'Programming_Language'))
    ruby.taggings.create!(tag: Tag.create(name: 'Dynamic_Typing'))

    rust.taggings.create!(tag: Tag.create(name: 'Programming_Language'))
    rust.taggings.create!(tag: Tag.create(name: 'Static_Typing'))
  end
  
  example 'You can define and use temporary classes' do
    expect(ruby.title).to eq 'Ruby'
  end
  
  example 'Of course you can also use relation' do
    expect(ruby.tag_names).to contain_exactly('Programming_Language', 'Dynamic_Typing')
    expect(Tag.find_by(name: 'Static_Typing').posts).to match_array([rust])
  end
end

Please check spec/*_spec.rb

Installation

Add this line to your application's Gemfile:

gem 'temporary_model'

And then execute:

$ bundle

Or install it yourself as:

$ gem install temporary_model

Contributing

Contribution directions go here.

testing

bundle exec rake
bundle exec rspec

License

The gem is available as open source under the terms of the MIT License.