1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Slack::Notifierをrspecでmockする

Posted at

SlackNotifierのmock方法の記事が見つからなったため書いてみました。

let(:slack_mock) { double('slack') }

subject { #処理を定義 }

# pingレシーバーを定義
before(:each) do
  allow(Slack::Notifier).to receive(:new).and_return(slack_mock)
  allow(slack_mock).to receive(:ping)
end

it 'Slack通知が実行されない' do
  expect(slack_mock).to receive(:ping).never
  subject
end

it 'Slack通知が1回呼び出される' do
  expect(slack_mock).to receive(:ping).once
  subject
end

# メッセージまで検証したい場合は下記の通り

it 'Slack通知に想定メッセージが渡され実行される' do
    expect_message = "test"
    expect(slack_mock).to receive(:ping).with(expect_message).once
  subject
end

参考記事

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?