Managing backtrace

I’m writing a method_missing handler to “automagically” create
assertions from predicate functions. So, for example, one use might be:

assert_not_include ‘[email protected]’, response.to

-or-

assert_include ‘[email protected]’, response.from

The code is working properly, but when I trigger an assert, the
backtrace shows something like:

  1. Failure:
    test_confirm(ContactMailerTest)
    [./test/unit/…/test_helper.rb:74:in method_missing' ./test/unit/../test_helper.rb:61:inmethod_missing’
    ./test/unit/contact_mailer_test.rb:39:in `test_confirm’]:
    assert_not_include: expected [email protected] not in [email protected]

What I’d prefer is to remove the items in the backtrace array that refer
to the test method because they don’t add information but am not certain
how. Rails uses clean_backtrace, which looks like it should work, but no
luck with that.

Any help appreciated.

I know it’s bad form to reply to one’s own post, but what I came up with
was to create yet another backtrace manager local to my code:

def scrap_backtrace(pattern)
yield
rescue Test::Unit::AssertionFailedError => e
path = File.expand_path(FILE)
puts e.class
raise Test::Unit::AssertionFailedError, e.message,
e.backtrace.reject { |line| File.expand_path(line) =~ /#{path}/ }
end

def method_missing(m, *args)
scrap_backtrace do
my_assert_code_that_shouldnt_show_in_backtrace
end
end

If there’s a cooler way to do this, I’d love to know.

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