Erik's Engineering

something alliterative

Bootstrapping a new Rails app with Ruby 1.9, Rails 3, Rspec and Cucumber

You're finally going to start on that new project, and you want to do it the right way. rvm, Ruby 1.9, Rails 3, rspec and cucumber. How to get started? Here's a quick rundown of the steps. I'm going to assume you've already got rvm and Ruby 1.9 installed. If you haven't, then please go here to install rvm and Ruby 1.9 first.

Bootstrapping Your Environment

First, set your terminal to a white background and no wider than the main column of your blog. Next, create a gemset and use it. rvm gemset create example, rvm use ruby-1.9.2-p0\@example. This will guarantee that only the right gems are available in the environment of your new project. We'll use an rvmrc to help automate this in a little bit. You can verify that your environment is clean with the 'gem list' command.

Next, you need to install rails. Now that rails 3 has been officially released, you can do that with the standard gem install rails command. No need for --pre or anything.

Now, create your application. Pass it the -T option (to skip test-unit), and specify your database engine. rails new example -T -d postgresql


. . .


Creating Your Application

Before you do anything else, cd into your new app and create your .rvmrc and .gitignore files. The .rvmrc will ensure that every time you move into your app, it switches to the right gemset. That way, you don't end up accidentally using the wrong ruby or installing gems where they'll interfere with other apps. Make sure your .rvmrc is working by moving out of and back into your app's directory. You should get a message about using your gemset.

# .rvmrc
rvm use ruby-1.9.2-p0@example


# .gitignore
.bundle
db/*.sqlite3
log/*.log
tmp/**/*
*~
.*#
.#*



Once you've got those, you can initialize your git repository and do an initial commit. You MAY wish to rename config/database.yml to config/database.yml.example first. That's a common pattern I've seen in a lot of apps, and it helps keep your actual passwords out of the git repository.

Adding Rspec and Cucumber

Since we're using Rails 3, all gem installs will go through bundler. Open up your Gemfile (helpfully created along with your Rails 3 app) and add the following:

group :test, :development do
  gem 'rspec-rails', '= 2.0.0.beta.20'
  gem 'shoulda'
  gem 'factory_girl_rails'
end

group :cucumber do
  gem 'capybara'           # better than webrat
  gem 'database_cleaner'   # clean database between tests
  gem 'cucumber-rails'
  gem 'cucumber'
  gem 'rspec-rails', '= 2.0.0.beta.20'
  gem 'spork'
  gem 'factory_girl_rails'
end

Don't try to include launchy or ruby-debug, as those aren't ready for use with Ruby 1.9 yet. Adjust the version of rspec-rails to get the most recent rev. Once you've got this added to your file, run bundle install. You may need to augment your PATH variable in order to get some gems to compile. Keep trying until you get the command to complete successfully.




Finally, you can run the new generators that rspec-rails and cucumber give you. These will set things up for you to run cucumber and rspec tests.


rails g rspec:install
rails g cucumber:install

Done

And that's it. Commit your code to git and start in on configuring your database, writing tests and generally getting things done with style.

Published on 11/09/2010 at 13h59 under , . Tags , , , ,

Comment Bootstrapping a new Rails app with Ruby 1.9, Rails 3, Rspec and Cucumber

Powered by Typo – Thème Frédéric de Villamil | Photo Glenn