RSpec
テスト方法がよくわからないので,とりあえずto_sqlして正規表現でちゃんとJOINされるか調べてみてる. describe User, type: :model do describe '.join_before_date_period' do subject { User.join_before_event } it do expect(subject.to_sql).to match…
同一のJOINを複数のscopeやメソッド,コンテキストで使用する場合, それ自体をscopeとして定義しておくとちょっとDRYになる. scope :join_before_date_period, -> do before = Event.arel_table.alias('before_event') joins( arel_table.join(before, Are…
shoulda matchers shoulda-matchersはvalidationやassociationのテストを超簡単に書ける便利マッチャ集みたいなgem. example class User < ActiveRecord::Base belongs_to :company has_one :profile delegate :name, to: :profile delegate :name, to: :co…
all mactcherを利用する. expect([1, 3, 5]).to all(be_odd) Compound Matcherも利用可能(Compound Matcherについては『今日から使える!RSpec 3で追加された8つの新機能』を参照されたい). expect([1, 3, 5]).to all(be_odd.and be < 10) expect([1, 4,…
Coverallsは(金銭的に)使えないけどCircleCIは使ってる,参考程度でいいからCoverage出したいという時に. colszowka/simplecovを利用する. # Gemfile group :test do # 手動requireするのでrequire: false gem 'simplecov', require: false end Circle C…
Controller Concernのテスト 例えばいいねアクションを追加するためのLikableというものがあるとする. これのテストをどうするか. # app/controllers/concerns/likable.rb module Likable extend ActiveSupport::Concern included do before_action :set_l…
yield matchersなるものがあるらしい. let(:array) { [1, 2, 3] } it { expect { |b| array.each(&b) }.to yield_control } it { expect { |b| array.each(&b) }.to yield_control.exactly(3) } it { expect { |b| array.each(&b) }.to yield_successive_ar…