3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

tempingつかってみた

Last updated at Posted at 2015-03-13

temping

concernでspecを書くときに、Dummyクラスを使いたいけど、
ActiveRecordの機能が必要なとき
tempingを使うことで、ActiveRecordなDummyクラスを使うことが出来る。

使い方

Gemfile
Gemfile
group :test do
  gem 'temping'
end
concern
app/models/concern/has_hoge_record.rb
module HasHogeRecord
  extend ActiveSupport::Concern
  module ClassMethods
    def default
      where(default: true).first
    end
  end
end
spec
spec/models/concern/has_hoge_record_spec.rb

require 'spec_helper'

describe HasHogeRecord do

  before(:all) do
    Temping.create :has_hoge_class do
      include HasHogeRecord
      with_columns do |t|
        t.boolean :default, default: false
      end
    end
  end

  describe :default do
    subject { HasHogeClass.default }
    let(:default) { HasHogeClass.create(default: true) }
    it { expect(subject).to eq default }
  end

end

※beforeでhas_hoge_classesテーブルが作成される。
before(:all)じゃないと、二回目のitでPG::UndefinedTableが発生。

地味に便利!

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?