DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

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

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
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Wrapping a JavaScript Library as a Well Tested Ruby Gem

Wrapping a JavaScript Library as a Well Tested Ruby Gem

Kanwaldeep 'kd' Singh Arneja user avatar by
Kanwaldeep 'kd' Singh Arneja
·
Apr. 01, 15 · Interview
Like (0)
Save
Tweet
Share
2.27K Views

Join the DZone community and get the full member experience.

Join For Free

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:


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 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.

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

GEM (desktop environment) JavaScript library integration test application

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

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: