Software engineer, data guy, Open Source enthusiast, New Hampshire resident, husband, father. Fan of guitars, hiking, photography, homebrewing, sarcasm.
Rails4 Javascript-integrated unit tests via PhantomJS, RSpec, and Capybara
In this blog post, I’ll share some code that sets up a basic configuration to do Javascript testing via RSpec and Phantomjs.
If you don’t already have PhantomJS, you can install it via Homebrew.
For this post, I ran a few commands to setup a new project space for a Rails app:
For my test I created a new controller called “Demos” with an action “index”:
I revised the default index file (app/views/demos/index.html.erb) and added an ID to the H1 tag. Later in the code I will use a JQuery selector to find this element and get its content.
I wanted to integrate user authentication with this test, so I added the devise gem. edit file: Gemfile, added:
Devise integration:
Updated the Demos controller to require an authenticated user, edit file: app/controllers/demos_controller.rb
Set the root/home route to point to the Demos controller index method. edit file: config/routes.rb, add:
Next I added the test gems. edit file: Gemfile, add:
Install the gems and run the RSpec generator:
Revised the RSpec helper to include the new gems and configuration, edit file: spec/spec_helper.rb
Created a FactoryGirl User factory, new file: spec/factories/user.rb
Created the integration unit test, new file: spec/features/demos_spec.rb