Testing for warnings

  • Thread starter Alex Gutteridge
  • Start date
A

Alex Gutteridge

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 Gutteridge

Bioinformatics Center
Kyoto University
 
J

Jano Svitok

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.
 
A

Alex Gutteridge

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.

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 Gutteridge

Bioinformatics Center
Kyoto University
 
G

Gregory Brown

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,311
Members
47,978
Latest member
ReneGibson

Latest Threads

Top