In RSpec, what’s the difference between before(:suite) and before(:all)?

When a before(:all) is defined in the RSpec.configure block it is called before each top level example group, while a before(:suite) code block is only called once. Here’s an example: RSpec.configure do |config| config.before(:all) { puts ‘Before :all’ } config.after(:all) { puts ‘After :all’ } config.before(:suite) { puts ‘Before :suite’ } config.after(:suite) { puts ‘After … Read more

Rspec testing redirect_to :back

Using RSpec, you can set the referer in a before block. When I tried to set the referer directly in the test, it didn’t seem to work no matter where I put it, but the before block does the trick. describe BackController < ApplicationController do before(:each) do request.env[“HTTP_REFERER”] = “where_i_came_from” end describe “GET /goback” do … Read more

Rails 3.1, RSpec: testing model validations

CONGRATULATIONS on you endeavor into TDD with ROR I promise once you get going you will not look back. The simplest quick and dirty solution will be to generate a new valid model before each of your tests like this: before(:each) do @user = User.new @user.username = “a valid username” end BUT what I suggest … Read more

How do I prepare test database(s) for Rails rspec tests without running rake spec?

I would recommend dropping your test database, then re-create it and migrate: bundle exec rake db:drop RAILS_ENV=test bundle exec rake db:create RAILS_ENV=test bundle exec rake db:schema:load RAILS_ENV=test After these steps you can run your specs: bundle exec rspec spec gerry3 noted that: A simpler solution is to just run rake db:test:prepare However, if you’re using … Read more

Set up RSpec to test a gem (not Rails)

I’ve updated this answer to match current best practices: Bundler supports gem development perfectly. If you are creating a gem, the only thing you need to have in your Gemfile is the following: source “https://rubygems.org” gemspec This tells Bundler to look inside your gemspec file for the dependencies when you run bundle install. Next up, … Read more