LoginSignup
6
3

More than 3 years have passed since last update.

enumのRSpecの書き方

Posted at

はじめに

enumのテストコードを書いたので、備忘として残します。
shoulda-matchersを使用しています。

ソースコード

app/models/hoge.rb
class Hoge < ApplicationRecord
  enum status: {
    hoge: 0,
    huga: 1
  }
end
spec/models/hoge_spec.rb
RSpec.describe Hoge, type: :model do
  describe 'enum' do
    it { is_expected.to define_enum_for(:status).with_values(hoge: 0, huga: 1) }
  end
end

解説

shoulda-matchersdefine_enum_forを使うと上のように書けます。
with_valuesではなく、withでもテストは通りますが怒られます。

簡単ですね!

参考

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

6
3
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
6
3