Test for warnings using RSpec

后端 未结 3 913
暗喜
暗喜 2021-01-17 23:32

Is it somehow possible to test warnings i Ruby using RSpec?

Like this:

class MyClass
  def initialize
    warn \"Something is wrong\"
  end
end

it \         


        
3条回答
  •  孤独总比滥情好
    2021-01-17 23:35

    There is a good article with custom expectation which solves exactly your problem: http://greyblake.com/blog/2012/12/14/custom-expectations-with-rspec/

    So it would like:

    expect { MyClass.new }.to write("Something is wrong").to(:error)
    

    Base on that article you can create you own expectation to use it like this:

    expect { MyClass.new }.to warn("Something is wrong")
    

提交回复
热议问题