Expanding from my previous post on a Grape API mounted on RACK.
Refactoring the Application Instance
Instead of sticking all of the Rack application code into config.ru, lets build a cleaner Acme::App _(in app/acme_app.rb). We’re going to drop _Rack::TryStatic and build this logic ourselves, since we might need to deal with other error codes than 404 (depending on your URL strategy you may be tripping over a 405). The logic remains the same: we try a bunch of static files and delegate to the API otherwise. You can also build primitive routing instead, so that everything requesting /api goes to the API and everything else goes to Rack::Static. Your mileage will vary.
RSpec API Tests
Now that we have an application class, we can add API and Capybara integration tests. We start with RSpec and Rack test gems in Gemfile.
The spec/spec_helper.rb adds Rack::Test.
Testing an API involves making requests on the Rack application, pretty straightforward.
RSpec Capybara Integration Tests
Notice that in the tests above we’re mounting the Rack application and making requests directly to it. Does it actually work in a browser? Do we see the public/index.html page?
We start by adding capybara into Gemfile. At the time of the writing we need to use the code from Capybara head, since it adds support for Capybara.app.
The spec/spec_helper.rb requires capybara/rspec, which brings in methods like page.visit and assigns an instance of the application to Capybara.app. Capybara will launch the application for us.
An integration test can go into spec/integration and must be marked with request: true _and _js: true (the latter forces the use of the Selenium driver that will popup a browser). Let’s look for a proper title on the homepage.
A POST, PUT and Some JQuery
The sample source in https://github.com/dblock/grape-on-rack also adds JQuery, extends the API to simulate a persisted counter, and makes PUT requests to it. Complete with an integration test. Run bundle install and bundle exec rackup _to see it and _bundle exec rspec spec to run the tests.
Backbone.js w/ Mongo
@knewter put together a neat Backbone.js + MongoDB w/ Mongoid demo using Grape that is built in a similar manner, https://github.com/knewter/grape_demo.