0

I'm writing a Ruby API for a C API which encapsulates a C++ library. The C API catches C++ exceptions thrown by the C++ library.

Ideally, I could go in and modify my C library so it raises Ruby exceptions, but since I'm using FFI, that's not really an option.

The C API prepends the exception string with "Caught exception: ", prints to STDERR, and then continues, basically ignoring the error. I'd like to watch for these types of strings in rspec.

Is this possible? Surely this has been done in rspec before, but I'm not quite sure how to search for this sort of functionality.

Translunar
  • 3,739
  • 33
  • 55

1 Answers1

0

You can try stubbing the STDERR as suggested in here:

  before do
    @orig_stderr = $stderr
    $stderr = StringIO.new
  end

  it "it writes to err" do
    subject.do_that_thing
    $stderr.rewind
    $stderr.string.chomp.should =~ "Caught exception: "
  end

  after do
    $stderr = @orig_stderr
  end
Community
  • 1
  • 1
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93