This page looks best with JavaScript enabled
⚠️

【RSpec】たまに使うちょっとトリッキーなマッチャ

 ·   ·  ☕ 1 分で読めます
✏️
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# subjectがArrayクラスであることを検証する
it { is_expected.to be_kind_of Array }

# 全要素がHashであることを検証する
it { is_expected.to all(be_kind_of Hash) }

# ハッシュの中身について検証する
it '各パラメータの値' do
  expect(Article.first.attributes.deep_symbolize_keys).to match(
    title: be_a(String),
    position: be_an(Integer),
    body: start_with('こんにちは'),
    environment: be_in(['development', 'test', 'production']),
    published: be_in([true, false]),
    created_at: be_a(ActiveSupport::TimeWithZone),
    updated_at: be_a(ActiveSupport::TimeWithZone),
  )
end
expect(Article.first).to have_attributes(
  title: 'タイトル',
  body: '本文'
)

# クラスに対して引数の検証を一部のみ行う
expect(HogeService).to receive(:call).with(
  message: include("メッセージの一部"),
  hoge: 'foo',
)
Share on

END
END
@aiandrox

 
目次