Ted Kulp

Code, Photos, Assorted Nonsense

Testing the Grape Microframework

Stumbled across the awesome Grape framework for writing a simple REST server for a small project I’m messing with. Since I was trying to write tests right out of the gate instead of using curl to do everything, I immediately started trying to get Rest::Test to work, which was failing miserably.

After googling and playing around for awhile, I did eventually get it. It was mainly with how to setup the app function, which didn’t make a lot of sense when config.ru uses the run function to get itself going.

I figured I’d just post this code below so if anyone else runs into this issue, they’ll have the answer. This is using MiniTest and Ruby 1.9.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
require 'helper'
require "rack/test"
class TestApi < MiniTest::Unit::TestCase
include Rack::Test::Methods
def app
Rack::Builder.new {
map "/" do
run Rest::API # The class that extends Grape::API
end
}.to_app
end
def test_it_gives_back_some_content
get '/rest/v1/content'
assert last_response.ok?
assert_equal '[]', last_response.body
end
end

Comments