我想測試一個巨人+葡萄應用程序像下面+葡萄:測試巨人使用RSpec
require 'em-synchrony/em-mongo'
require 'yajl/json_gem'
require 'goliath'
require 'grape'
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
這兩個類都在名爲單個文件app.rb.運行ruby ./app.rb -sv
在http://0.0.0.0:9000/v1/categories/
上啓動了一個鉅作應用程序,該應用程序非常好用,但在製作更復雜的終端之前需要rspec。反正運行規範,我得到
未定義的局部變量或方法'應用」
這我無法擺脫的:
$ bundle exec rspec spec/api_spec.rb
FFFF
Failures:
1) App App GET /v1/categories get several categories of repositories by name
Failure/Error: get "/v1/categories"
NameError:
undefined local variable or method `app' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x993eec4>
# ./spec/api_spec.rb:9:in `block (4 levels) in <top (required)>'
的api_spec如下所示:
require 'spec_helper'
describe App do
include Rack::Test::Methods
describe App 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
更新:
添加應用程序的方法,以投機/ api_spec.rb:
def app
App
end
上升另一種錯誤的:
1) App App GET /v1/categories get several categories of repositories by name
Failure/Error: get "/v1/categories"
NoMethodError:
undefined method `call' for App:Class
# ./spec/api_spec.rb:13:in `block (4 levels) in <top (required)>'
UPDATE
規格/ api_spec內從應用方法被稱爲加入API類。 rb:
def app
API
end
get undefined method
mongo'`:
Failures:
1) App App GET /v1/categories get several categories of repositories by name
Failure/Error: get "/v1/categories"
NoMethodError:
undefined method `mongo' for #<Hash:0xad5ea58>
# ./app.rb:14:in `block (2 levels) in <class:API>'
# ./spec/api_spec.rb:13:in `block (4 levels) in <top (required)>'
看到coll = env.mongo.collection('categories')
API類中
不能正常工作,請參閱更新的主要問題: – 2013-02-23 15:42:38
我不知道什麼歌利亞。但似乎它應該工作,如果你使用API類而不是App – 2013-02-23 21:08:38
現在我得到未定義的方法'mongo'請參見上次UPDATE – 2013-02-24 08:16:34