RSpec / Ruby / Rails

連載: Rails4のActiveRecord向けRSpecカスタムマッチャ5選

このカスタムマッチャを使うと

describe Person do
  it { should create_model }
  it { should create_model.for(2).times }
end

と書くことで

$ bundle exec rspec spec/models/person_spec.rb
Person
  should create 2 records
  should create 1 record

とテストできるようになります。

カスタムマッチャは

RSpec::Matchers.define :create_model do
  def for(n)
    @number = n
    self
  end

  def times
    @create_count = @number
    self
  end

  match do |model|
    klass = model.class
    name = klass.table_name.singularize

    @create_count ||= 1

    before_count = klass.count

    @create_count.times { create(name) }

    @created_count = klass.count - before_count
    @created_count == @create_count
  end

  description { "create #{@created_count} #{"record".pluralize(@created_count)}" }
  failure_message { "expected to create #{@created_count} #{"record".pluralize(@created_count)}, but created #{@created_count}" }
end

となります。

関連記事