Re: [goliath] Need help testing Goliath/Grape wit Rspec

143 views
Skip to first unread message

Eric Marden

unread,
Feb 26, 2013, 8:56:21 AM2/26/13
to golia...@googlegroups.com
First make sure you have this in your spec_helper.rb

require 'goliath/rack'
require 'goliath/test_helper'
Goliath.env = :test


Then, try testing your API like this:

describe API do
  describe 'GET /v1/categories' do
    it 'get several categories of repositories by name' do
    with_api(API) do
      get_request(:path => "/v1/categories") do |request|
        request.status.should == 201
        p (JSON.parse r.response)
      end
    end
  end  
end





-- 
Eric Marden

On Monday, February 25, 2013 at 1:02 PM, Luca G. Soave wrote:


Hi, I'm having hard time trying to make rspec working on this app.rb goliath endpoint server :

require 'goliath'
require 'grape'
require 'yajl/json_gem'
require 'em-synchrony/em-mongo'

class API < Grape::API
 
  version 'v1', :using => :path
  format :json
 
  resource 'categories' do
    # http://0.0.0.0:9000/v1/categories/
    get "/" do
      coll = env.mongo.collection('categories') #Connection Pool from Goliath ENV
      coll.find({})
    end
  end
end

class App < Goliath::API
  def response(env)
    API.call(env)
  end
end

I would need something like  :

require 'spec_helper'
require 'yajl/json_gem'

describe App do
  include Rack::Test::Methods

  def app
    API
  end

  describe API do
    describe 'GET /v1/categories' do
      it 'get several categories of repositories by name' do
        get "/v1/categories"
        last_response.status.should == 200
        JSON.parse(last_response.body)["name"].should == "Ruby Web Frameworks"
      end 
    end 
  end
end

but this simpli dosn't work. I also had a look at spec/integration examples without success.

Can someone please show me the right way ?

--
You received this message because you are subscribed to the Google Groups "Goliath.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to goliath-io+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Luca G. Soave

unread,
Mar 1, 2013, 11:55:08 AM3/1/13
to golia...@googlegroups.com

Thank you Eric,
goliath test_helper finally put me on the right way, it works.

In spec/spec_helper.rb I added goliath test helper and all dependencies. In my case :

require 'em-synchrony/em-http'
require 'goliath/test_helper'
require 'yajl/json_gem'

Goliath.env = :test

RSpec.configure do |c|
  c.include Goliath::TestHelper, :example_group => {
    :file_path => /spec\//
  }
end

in spec/app_spec.rb 

require 'spec_helper'
require File.join(File.dirname(__FILE__), '../', 'app')

describe App do
  def config_file
    File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'app.rb'))
  end

  let(:api_options) { { :config => config_file } }

  it 'renders ' do
    with_api(App, api_options) do
      get_request(:path => '/v1/categories') do |c|
        resp = JSON.parse(c.response)
        categories = resp.map{|r|r['name']}
        categories.to_s.should =~ /Ruby Web Frameworks/
      end
    end
  end
end

Eric Marden

unread,
Mar 1, 2013, 2:25:36 PM3/1/13
to golia...@googlegroups.com
Glad it worked out for you :)

I forgot that testing stuff is documented here: https://github.com/postrank-labs/goliath/wiki/Testing 

Let me know if anything on that page is missing/confusing.


-- 
Eric Marden

Reply all
Reply to author
Forward
0 new messages