Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

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
jrits
信頼と魅力のある先進のITをもとに、お客様のワークスタイル・イノベーションの実現を目指します。
http://www.jrits.co.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした