Minitest Text-to-speech (pt. 1)

______

by Matt

Listen to your tests. Literally! Getting text-to-speech in your MiniTest.

(Note: I don't suggest actually doing this. I'm putting together a Ruby Gem which will make this incredibly easy for you to get speech into your tests. Consider this just part of the process on how I figured out how to get my MiniTest talking.)

The lightning talk, given during Code Fellows Ruby on Rails bootcamp: My Code Fellows lightning talk

minitest-tts

Step-by-step instructions to get spoken alerts into your minitests.

1) Make sure the minitest gem is installed.

  • These instructions assume minitest v.4.7.5.
  • Write some tests for minitest. Using RSpec style here from RailsCast #327 “MiniTests with Rails” project.

2) Install speaker gem.

  • Based on Google Translator API.
  • gem 'speaker' in :test group (using v.0.0.4 here) then $ bundle install
  • Creates audio.mp3 file in project’s main directory. No need to add to GitHub.
  • To play mp3 file, Macs use afplay. May already be installed. Ubuntu uses mpg123. Need to install that if you use Ubuntu. $ sudo apt-get install mpg123

3) To receive alert when a specific test is about to run:

  • In the test file, add Speaker.new(text: "Your test is about to run. Come hither!!").tts
    directly before test is going to be run. Example:

        describe ProductsHelper do
          it "converts number to currency" do
            Speaker.new(text: "converts number to currency").tts
            number_to_currency(5).must_equal "$5.00"
          end
    

4) To receive alerts after each test has completed:

  • Modify minitest/unit.rb file. (somewhere around this directory: ~/.rvm/gems/ruby-2.0.0-p247@rails3/gems/minitest-4.7.5/lib/minitest)
  • May have to use sudo in order to modify the file: $ sudo subl unit.rb
  • Add speaker code directly into minitest/unit.rb file, around line 1219 in the after_teardown hook:

         def after_teardown;
           Speaker.new(text: "one test complete").tts
         end
    

5) To receive alert when all the tests have completed:

  • Add speaker code directly into the minitest/unit.rb file (sudo again), around line 891:

        puts "Finished #{type}s in %.6fs, %.4f tests/s, %.4f assertions/s." %
          [t, test_count / t, assertion_count / t]
    
        Speaker.new(text: "Your tests are done. Stop playing xbox.").tts
    
        report.each_with_index do |msg, i|
          puts "\n%3d) %s" % [i + 1, msg]
        end
    

6) Done! Rake and enjoy the ability to stretch your legs and know exactly when your tests have been completed.

Shippable CI tips

______

by Matt

A couple of tips for getting Shippable working with your Ruby on Rails Heroku-hosted project.

Shippable is a fast new continuous integration / continuous deployment service that, in my experience, is also one of the easiest to use. I installed it in my latest project a few days ago and thought I'd share a couple of tips. My project is a Ruby on Rails (Rails3) app hosted on Heroku using GitHub revision control. This is probably a similar setup to many, if not most, potential Shippable users, so I hope my experience will be helpful. I only hit a couple of snags that may hang you up as well.

1) PostgreSQL database setup 2) secret_token handling

The first problem I had was the database setup. The first time I ran Shippable, I got this error:

psql: FATAL: Peer authentication failed for user "matt"

Seems "matt" can't access the database. Turns out he doesn't need to. Here is how I got PostgreSQL working with Shippable (with thanks to the TravisCI docs - Shippable shares much in common with Travis).

First, create a new file called database.shippable.yml and place it in your config directory. Copy everything from your database.yml file to the new file, but make sure the username is Postgres and the password is blank (that's the stock username/password for PostgreSQL and all we need for testing). The file should look something like this now:

config/database.shippable.yml:

development:
  adapter: postgresql
  encoding: unicode
  database: appname_development
  pool: 5
  username: postgres
  password:

test:
  adapter: postgresql
  encoding: unicode
  database: appname_test
  pool: 5
  username: postgres
  password:
  min_messages: warning

production:
  adapter: postgresql
  encoding: unicode
  database: appname_production
  pool: 5
  username: yourname
  password:

Great. Now we're ready to make sure Shippable can use that information, so let's open up your shippable.yml file in your root directory. Make sure you have the following line in the 'before_script' section:

shippable.yml:

before_script:
  - cp config/database.shippable.yml config/database.yml

That will copy over the Shippable database file into the original database file. I like having the separate file so I always know where the code came from and why it was needed. Now our database is almost ready to go, but we need to make sure the database is going to be created and ready to run your tests. And once the database is created, we also need to make sure every migration is run, so our shippable.yml file's 'before_script' section will end up looking like this:

shippable.yml:

before_script:
  - cp config/database.shippable.yml config/database.yml
  - psql -c 'create database acroparty_test;' -U postgres
  - rake db:migrate

There we go, the PostgreSQL problem should now be solved.

Now, the second problem I had was figuring out what to do with the secret_token of my project. I had used Figaro to move my secret_token out of the public eye, but that broke things for Shippable. The error I got when running my project through Shippable was:

A secret is required to generate an integrity hash for cookie session data. Use config.secret_token = "some secret phrase of at least 30 characters"in config/initializers/secret_token.rb

Shippable didn't have access to that environment variable, so things came to a screeching halt. I tried for a while to find a way to pass that secret_token to Shippable (Travis has a gem which can hide that variable), but in the end realized it didn't matter what the token was since it was just needed for the tests. So a quick bit of code in the secret_token.rb file fixes this problem:

config/initializers/secret_token.rb:

Yourapp::Application.config.secret_token = if Rails.env.test?
  ('x' * 30)
else
  ENV['SECRET_TOKEN']
end

Now when in the test environment, the secret_token is a bunch of x's. Problem solved!

I hope these two tips can help you get Shippable up and running in your Rails project. The two big strengths for Shippable are its speed (my last run lasted a whopping 39 seconds) and its ease of use. Allowing write access to your project is always a little scary, but in the end I think it saves some headaches and is worth it.

I'll be using Shippable to deploy to Heroku in the coming weeks and if anything comes up there I'll be sure to let you know.


Last posts

___

Shthumbnail

Shippable CI tips

A couple of tips for getting Shippable working with your Ruby on Rails Heroku-hosted project.

Yoda

has_many :through Tutorial

A step-by-step tutorial on how to make a has_many :through association in Ruby on Rails.

Billthecatthumb

Minitest Text-to-speech (pt. 1)

Listen to your tests. Literally! Getting text-to-speech in your MiniTest.


Scroll top