27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The projects API<br />

The first element of this result is the HTTP status code, and in this case it indicates<br />

that your fictional response was 200, or in human-terms: OK. The second element contains<br />

no special HTTP headers, but you’ll see these as you progress in the next section.<br />

Finally, the third element contains a string, which represents the body of this request,<br />

returning the string “Hello World!”<br />

Using Rack::Test::Methods, you can initiate requests to your application’s API<br />

that then return these Rack responses, and you can then use these responses to check<br />

that your API is responding in the correct way. You’re purposely not using the standard<br />

RSpec controller tests here to make sure that the precise URLs are responding in the<br />

correct manner, instead of only testing that the actions are doing what you tell them.<br />

Let’s get into writing this initial test for your API.<br />

13.1.1 Your first API<br />

You’re going to continue on the running theme of “test everything” with your API, and<br />

with the rack-test gem you’ve got the necessary tools to test what your API’s URLs are<br />

doing. Rather than testing this in Cucumber, you’re instead going to use RSpec, which<br />

provides a nicer DSL for testing your APIs. To begin with, you’re going to create a new<br />

folder structure at spec/apis/v1. You should name the apis directory as a plural for two<br />

reasons: first, it matches the consistency of the other directories in the spec directory;<br />

and second, it may one day contain more than one API version. Then you’ll create a<br />

new file called spec/apis/v1/projects_spec.rb and begin to fill it with the following:<br />

require "spec_helper"<br />

describe "/api/v1/projects", :type => :api<br />

do<br />

end<br />

There’s much more code to come after this short snippet, but it’s a pretty good start.<br />

In the describe block here you pass through an option of :type => :api. What<br />

does this do? Well, you can use it to modify the behavior of this describe block in<br />

many ways, such as including modules. The Rack::Test::Methods module you’re<br />

going to use needs to be included into each test. Rather than doing this manually, you<br />

can use this :type option to do the include for you, as well as some additional behavior.<br />

Let’s open a new file at spec/support/api/helper.rb and put the content from the<br />

following listing inside.<br />

Listing 13.2 spec/support/api/helper.rb<br />

module ApiHelper<br />

include Rack::Test::Methods<br />

def app<br />

Rails.application<br />

end<br />

end<br />

351

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!