Wrapping a JavaScript Library as a Well Tested Ruby Gem

DZone 's Guide to

Wrapping a JavaScript Library as a Well Tested Ruby Gem

· ·
Free Resource

In order to help a team mate, I recently gave it shot writing a Ruby gem, “angular-translate-rails". 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 gem. 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 are basically.

test code

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 actual tests:

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
We simply request the library as if we were in the browser and check the response if was a success.

This tests if the gem being offered as a Rails module or not.  These lines here the the crux of this test.
test "truth" do
  assert_kind_of Module, AngularTranslate::Rails
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.

The gem repo is here  if you need to fork and play with it.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}