我正在为封装 C++ 库的 C API 编写 Ruby API。C API 捕获 C++ 库抛出的 C++ 异常。
理想情况下,我可以进入并修改我的 C 库,以便引发 Ruby 异常,但由于我使用的是 FFI,所以这不是一个真正的选择。
C API 在异常字符串前面加上“Caught exception:”,打印到 STDERR,然后继续,基本上忽略了错误。我想在 rspec 中查看这些类型的字符串。
这可能吗?当然这已经在 rspec 中完成了,但我不太确定如何搜索这种功能。
您可以尝试按照此处STDERR
的建议存根:
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