Wrapping a JavaScript Library as a Well Tested Ruby Gem
Join the DZone community and get the full member experience.
Join For FreeIn 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.
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.
1. resource_test.rb
test 'can access angular-translate' do get '/assets/angular-translate.js' assert_response :success end
2. angular-translate-rails_test.rb
test "truth" do assert_kind_of Module, AngularTranslate::Rails end
The gem repo is here if you need to fork and play with it.
Opinions expressed by DZone contributors are their own.
Trending
-
A React Frontend With Go/Gin/Gorm Backend in One Project
-
Turbocharge Ab Initio ETL Pipelines: Simple Tweaks for Maximum Performance Boost
-
What to Pay Attention to as Automation Upends the Developer Experience
-
Step Into Serverless Computing
Comments