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

Working with JSON (Intermediate)

JSON is a lightweight text-based communication medium that commonly appears in place of XML in newer web applications and web services.

For this exercise, let's pretend that our web application has a sibling mobile application that will transmit geographic coordinates and the application needs an API, which it can communicate with.

How to do it...

  1. Begin with updates to spec/controllers/locations_controller_spec.rb:
    context "when JSON format" do
      describe "#create" do
        subject { post :create, { :format => :json,
                   :location => {
                      :latitude => 25.0,
                      :longitude => -40.0 }
                }
        }
        its(:status) { should == 200 } # OK
      end
    end

    Note

    This looks a lot like our normal create method except as an API endpoint, we expect a 200 and not a

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