Augmented Usamimi

it { is_expected.to be_blog.written_by(izumin5210) }

RSpec

【Rails】JOINするscopeのテスト

テスト方法がよくわからないので,とりあえず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…

【Rails】JOINするscopeを書く

同一のJOINを複数のscopeやメソッド,コンテキストで使用する場合, それ自体をscopeとして定義しておくとちょっとDRYになる. scope :join_before_date_period, -> do before = Event.arel_table.alias('before_event') joins( arel_table.join(before, Are…

【RSpec】shoulda-matchersでenum,delegateのテスト

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…

【RSpec】配列要素全てにマッチャを適用する

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,…

CircleCIにcoverageを出す

Coverallsは(金銭的に)使えないけどCircleCIは使ってる,参考程度でいいからCoverage出したいという時に. colszowka/simplecovを利用する. # Gemfile group :test do # 手動requireするのでrequire: false gem 'simplecov', require: false end Circle C…

【Rails】Controller Concernのテスト

Controller Concernのテスト 例えばいいねアクションを追加するためのLikableというものがあるとする. これのテストをどうするか. # app/controllers/concerns/likable.rb module Likable extend ActiveSupport::Concern included do before_action :set_l…

【RSpec】yieldを伴うメソッド(ブロック引数 etc.)のテスト

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…