How To Test a Ruby Gem That Wraps a JavaScript Library

In order to help a teammate, I recently gave it shot writing a public Ruby Gem, “angular-translate-rails“. For the newbies, the idea is that if you are working inside a Ruby on Rails application and you would like to include an asset. That is done by acquiring it as a Ruby Gem aka a glorified dependency. The main advantage is that you do not have to worry about maintaining it in your own code base, including its licenses. If you want to understand more about gems and how to create them, this is a great resource.

Now, the gem I created started out basically as a wrapper of a JavaScript library called “angular-translate”. The documentation on creating the gem is helpful and building it was easy, but it was the testing that took most of my time. So let me speak to that mainly.

A spec to test the internals of the library are really not what was needed it. That seem to have been done well in the library itself. What I wanted to test was that the gem will do what it is supposed to do, i.e. deliver the asset to the application in the right place when included.

I am not going to go into every detail of the test but here is the high level overview.

I see mainly two ways to test the gem like this, i.e. 3rd party JavaScript library wrapper.

Scenario A. Create a vanilla rails application and make it use the gem. That is it! The steps basically include:

$ rails new angular-translate-gem-test-app
$ cd angular-translate-gem-test-app
$ echo 'gem “angular-translate-rails", :path => “/Path To Gem Directory"' >> Gemfile
$ bundle install
$ echo '//= require angular-translate' >> app/assets/javascripts/application.js
$ rails server &
$ curl http://localhost:3000/assets/angular-translate.js
$ fg
<ctrl-c>

Scenario B. Here, if you created the gem as a standard Ruby module with default scaffolding, you will see a test directory with a dummy application. Think of this as a bundled application serving as part of an integration test that tries to mimic using the gem.

The dummy application is defined in application.rb with defaults. It has all the bolts and nuts it needs i.e. environments and initializers.

The are two main tests:

1. resource_test.rb

This is the integration test which will run the dummy app on the fly, request the library and check if the app serves the asset with correct version.

This line here is the heart of this test.

test 'can access angular-translate' do
...get '/assets/angular-translate.js'
...assert_response :success
end

We simply request the library as if we were in the browser and check the response if was a success.

2. angular-translate-rails_test.rb

This tests if the gem being offered as a Rails module or not.

These lines here are the the crux of this test:

test "truth" do
...assert_kind_of Module, AngularTranslate::Rails
end

Usually, going with Scenario A should be more than enough to test the integrity of the gem. But personally as I learn more about the Rails world, which has way to many moving parts, it infuses more confidence with in me and perhaps with other developers using this gem that a test is bundled within the gem.

Here is my gem repo in case you feel like forking and playing with it.

Leave a comment