O'Reilly logo

Instant RSpec Test-Driven Development How-to by Charles Feduke

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Testing Rails controllers (Intermediate)

In this recipe we'll see how to test drive the development of a Rails controller.

How to do it...

  1. Begin with LocationsController#create in spec/controllers/locations_controller_spec.rb:
    describe LocationsController do
      describe "#create" do
        subject { post :create, { :location =>
          { :latitude => 25.0,
            :longitude => -40.0 }
          }
        }
        its(:status) { should == 302 } # redirect
      end
    end
  2. Running this fails because there's no #create method in LocationsController. Rectify this by adding it to app/controllers/locations_controller.rb:
    class LocationsController < ApplicationController
      def create
      end
    end
  3. Executing rspec now reveals that we're failing because the view template is missing. By default, Rails will look for a template ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required