I think your method of testing is fine.
My app is also a single-page Backbone/Rails 3.1 app, and my tests are similar to yours. My tests are a bit more broken down -- I generally aim for one assertion per
specify block -- but the substance of the tests amounts to the same thing: make a request, parse the response, and make assertions based on what the response contains versus what I expect that it should contain.
The crucial question to ask yourself here is what idiomatic controller tests might look like in a more traditional, multi-page, non-Backbone app. You're generally checking a few things in those tests:
- A particular value is assigned
- A particular template is rendered, or that you've redirected to a particular URL
- The response code matches your expectation
For item 1, I think that amount to parsing the response and looking for a particular value. RSpec-Rails gives you the nice sugar of the
assigns statement, which makes things look a bit cleaner, but in my mind the intent is the same as what you're doing. You could pretty easily write your own sugar to wrap the parsed JSON response body, if you wanted, so you could write something like
json_response(:name).should == @account_one.name, where
json_response is the name of the helper method you would write. (The implementation basically amounts to parsing the response body and looking up the value for the key passed as a method argument.) Do that and the tests look a lot more like those for a non-Backbone app.
Item 2 doesn't apply to a single-page Backbone app, since you're always on the same page. So that's out.
Item 3 is the same as in a non-Backbone app. You have
assert_response(:success), which is fine. I like to do something like
response.status.should eq(200), or whatever is appropriate for the status code I expect. I find that I check status codes more often in my Backbone app's tests than in a more "traditional" multi-page app, just because I like to return appropriate HTTP status codes when possible as error response in Ajax apps, whereas in a multi-page app I might be returning a 200 status with some error message on the page like "Email address is mandatory." YMMV, in this regard.