17
13

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 5 years have passed since last update.

rspecで特定のログが吐かれていることを確認する

Last updated at Posted at 2014-02-12

rspecでログのテスト備忘録

  • Railsの特定のログがinfoで一回だけ呼ばれることをテストしたい
  • 処理の途中経過やベンチマーク結果等が出ていることを確認したい
  • rspecでRails.logger.should_receive(:info).with(expected).onceのように書きたい
  • これだと、infoで呼ばれたすべてのログに対してチェックしてしまう

解決法

  • Rails.logger.stub(:info => nil)を使う
  • withで渡したログ形式とマッチした回数をテストできる
  before do
    Rails.logger.stub(:info => nil)
    expected = {
      :type     => "benchmark",
      :category => "upload",
      :realtime => anything
      }
    Rails.logger.should_receive(:info).with(expected).once
  end
17
13
1

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
17
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?