Insomnia

Setting up Rspec & Rails

Along with my new setup for a Rails / PostgreSQL application, I have included RSpec tests instead of the default test:unit.

I always seem to have difficulty remembering the steps to get RSpec tests generated as part of ‘rails generate’ and getting autotest to work. These steps work on both Windows, Mac and probably Linux

Rspec

  1. Use —skip-test-unit as an argument on the rails new command.
  2. Include gem ‘rspec-rails’, ‘2.8.1’ in the Gemfile
  3. Run bundle install.
  4. Run rails generate rspec:install

This will generate a spec/directory and a spec/spec_helper.rb file. Run bundle exec rspec spec/ now and you’ll see test output (even though there aren’t any tests yet).

Autotest

Autotest on Windows XP won’t show any output if no tests have been created. To me, I thought it was hung. Once I ran rails generate model grain name:text:, then Autotest produced output from the generated specs.

Something I didn’t have to do:

  • At some point I installed ZenTest with gem. Consequently gemtest works, but bundle exec autotest doesn’t since I don’t have ZenTest in my GemFile. Once I put ZenTest in my Gemfile, Autotest works as I expect.
gem 'ZenTest', '4.6.2'

Extras

On Mac, you can install Growl support and fsevent by including those gems in your Gemfile and then creating a /.autotest file with the following contents:

require 'autotest/fsevent'
require 'growl'

On Linux, use iNotify. Add

autotest-inotify
to Gemfile and
require ‘autotest/inotify’ 
to .autotest

Rails / PostgreSQL tips

I have been switching between Sqlite and PosgreSQL for my Rails applications. Considering how easy it’s been to move a sqlite3 dev database to Heroku hosted PostgreSQL, I haven’t had much need for PostgreSQL lately. However, I’d like to avoid any surprises because of DB-specific SQL.

So, I just created a Rails/PostgreSQL app, and am documenting the steps for posterity.

  1. Create a Rails app with ‘rails new myapp -d postgresql’
  2. Create database
    • If you haven’t already created a user yet, look at this page Setup Rails
    • Note: That creates a role in template1 (Read up on templates in PostgreSQL if you’re not familiar
    • Create the databases, either with DDL, like in the previou link, or from the shell like in this example (Create Database)
  3. Modify config/database.yml with the username/password that you just created
  4. Then run ‘bundle exec rake db:migrate’