Testing for warnings

Is there a way of asserting that a warning is raised in a unit test?
So I could write something like:

assert_warns “Blah blah” do function_that_warns(foo) end

I can’t seem to find anything in the test/unit docs, but it seems
like something that would be relatively common to test for…

Alex G.

Bioinformatics Center
Kyoto University

On 7/2/07, Alex G. [email protected]d wrote:

Is there a way of asserting that a warning is raised in a unit test?
So I could write something like:

assert_warns “Blah blah” do function_that_warns(foo) end

I can’t seem to find anything in the test/unit docs, but it seems
like something that would be relatively common to test for…

You may try to write that yourself by replacing Kernel#warn, though
this way you cannot check warnings from C code. Alternatively you
could replace/capture $stderr/STDERR and look for warnings there.

On 3 Jul 2007, at 06:23, Jano S. wrote:

this way you cannot check warnings from C code. Alternatively you
could replace/capture $stderr/STDERR and look for warnings there.

I specifically need to check for warnings from C code so replacing
Kernel#warn won’t work. I will try hooking into STDERR though -
thanks for the idea.

Alex G.

Bioinformatics Center
Kyoto University

On 7/2/07, Alex G. [email protected] wrote:

I specifically need to check for warnings from C code so replacing
Kernel#warn won’t work. I will try hooking into STDERR though -
thanks for the idea.

Possibly make use of Mocha or another mocking framework?

class Foo
def bar
warn “baz”
end
end

require “test/unit”
require “rubygems”
require “mocha”

class TestFoo < Test::Unit::TestCase

def test_bar_should_warn
a = Foo.new
a.expects(:warn).with(“baz”)
a.bar
end

end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs