Let’s say you usually code in Ruby, and your company and its build systems are
built around Rakefiles and the like. Today you’ve written some C code, and you
want to add unit tests. In this blog post, I present a method of writing those
C unit tests in Ruby using FFI and
RSpec.
First, compile your C code as position independent and symbols exported. This allows you to dlopen() the executable:
Next, add the FFI gem to your Gemset, in Gemfile:
Then write your rspec tests spec/foo_spec.rb:
I’m excited about this approach because the tests run under rspec along with the rest of your spec tests.