Ruby 测试歌利亚&x2B;带Rspec的葡萄
我正在测试一个Goliath+Grape应用程序,如下所示:Ruby 测试歌利亚&x2B;带Rspec的葡萄,ruby,testing,rspec,grape,goliath,Ruby,Testing,Rspec,Grape,Goliath,我正在测试一个Goliath+Grape应用程序,如下所示: 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:900
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
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_规范如下所示:
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
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
更新:
将应用程序方法添加到spec/api_spec.rb:
def app
App
end
def app
API
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)>'
获取未定义的方法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)>'
故障:
1) App-App-GET/v1/categories按名称获取多个类别的存储库
失败/错误:获取“/v1/类别”
命名错误:
未定义的方法“mongo”#
#./app.rb:14:in‘block(2层)in’
#./spec/api_spec.rb:13:in'block(4层)in'
请参见API类内部的
coll=env.mongo.collection('categories')
我认为您必须在测试中提供一种方法来指定应用程序的类
def app
App
end
更新
改用API类
def app
API
end
我认为您必须在测试中提供一个方法来指定应用程序的类
def app
App
end
更新
改用API类
def app
API
end
我终于能够用它工作了。这是为了跟踪的目的,希望它能帮助其他人 在spec/spec_helper.rb中添加goliath测试助手和所有依赖项。就我而言:
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
规范内/应用规范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
Goliath应用程序目录树,如下所示:
lsoave@ubuntu:~/rails/github/GGM$ ls -l
total 48
-rw-rw-r-- 1 lsoave lsoave 483 Feb 25 23:06 app.rb
-rw-rw-r-- 1 lsoave lsoave 6321 Feb 25 23:06 categories.json
drwxrwxr-x 2 lsoave lsoave 4096 Feb 25 23:06 config
-rw-rw-r-- 1 lsoave lsoave 381 Feb 25 23:06 Gemfile
-rw-rw-r-- 1 lsoave lsoave 2293 Feb 25 23:06 Gemfile.lock
-rw-rw-r-- 1 lsoave lsoave 59 Feb 21 20:37 Procfile
-rw-rw-r-- 1 lsoave lsoave 123 Feb 25 23:06 Rakefile
-rw-rw-r-- 1 lsoave lsoave 7003 Feb 21 20:37 README.md
-rw-rw-r-- 1 lsoave lsoave 238 Feb 25 23:06 README.mongoimport
drwxrwxr-x 2 lsoave lsoave 4096 Feb 25 23:23 spec
lsoave@ubuntu:~/rails/github/GGM$
其中,配置和规范子项看起来像:
lsoave@ubuntu:~/rails/github/GGM$ ls -l config spec
config:
total 4
-rw-rw-r-- 1 lsoave lsoave 870 Feb 25 23:06 app.rb
spec:
total 11
-rw-rw-r-- 1 lsoave lsoave 777 Feb 25 23:06 app_spec.rb
-rw-rw-r-- 1 lsoave lsoave 218 Feb 25 23:06 spec_helper.rb
lsoave@ubuntu:~/rails/github/GGM$
goliath主应用程序与我的第一篇帖子相同:
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
要求“em同步/em mongo”
需要“yajl/json_gem”
需要“歌利亚”
需要“葡萄”
类API:路径
格式:json
资源“类别”是什么
# http://0.0.0.0:9000/v1/categories/
得到“/”做
coll=env.mongo.collection('categories')#来自Goliath env的连接池
coll.find({})
结束
结束
结束
类应用程序
最后,准备工作很简单。如果您想更深入地了解,请看一看。我终于能够使用。这是为了跟踪的目的,希望它能帮助其他人 在spec/spec_helper.rb中添加goliath测试助手和所有依赖项。就我而言:
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
规范内/应用规范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
Goliath应用程序目录树,如下所示:
lsoave@ubuntu:~/rails/github/GGM$ ls -l
total 48
-rw-rw-r-- 1 lsoave lsoave 483 Feb 25 23:06 app.rb
-rw-rw-r-- 1 lsoave lsoave 6321 Feb 25 23:06 categories.json
drwxrwxr-x 2 lsoave lsoave 4096 Feb 25 23:06 config
-rw-rw-r-- 1 lsoave lsoave 381 Feb 25 23:06 Gemfile
-rw-rw-r-- 1 lsoave lsoave 2293 Feb 25 23:06 Gemfile.lock
-rw-rw-r-- 1 lsoave lsoave 59 Feb 21 20:37 Procfile
-rw-rw-r-- 1 lsoave lsoave 123 Feb 25 23:06 Rakefile
-rw-rw-r-- 1 lsoave lsoave 7003 Feb 21 20:37 README.md
-rw-rw-r-- 1 lsoave lsoave 238 Feb 25 23:06 README.mongoimport
drwxrwxr-x 2 lsoave lsoave 4096 Feb 25 23:23 spec
lsoave@ubuntu:~/rails/github/GGM$
其中,配置和规范子项看起来像:
lsoave@ubuntu:~/rails/github/GGM$ ls -l config spec
config:
total 4
-rw-rw-r-- 1 lsoave lsoave 870 Feb 25 23:06 app.rb
spec:
total 11
-rw-rw-r-- 1 lsoave lsoave 777 Feb 25 23:06 app_spec.rb
-rw-rw-r-- 1 lsoave lsoave 218 Feb 25 23:06 spec_helper.rb
lsoave@ubuntu:~/rails/github/GGM$
goliath主应用程序与我的第一篇帖子相同:
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
要求“em同步/em mongo”
需要“yajl/json_gem”
需要“歌利亚”
需要“葡萄”
类API:路径
格式:json
资源“类别”是什么
# http://0.0.0.0:9000/v1/categories/
得到“/”做
coll=env.mongo.collection('categories')#来自Goliath env的连接池
coll.find({})
结束
结束
结束
类应用程序
最后,准备工作很简单。如果您想深入了解,请查看。添加其他帖子示例:
require 'spec_helper'
describe V1::API do
it "login" do
with_api V1::Login do
post_request(path: "/v1/login", :head => {'authorization' => HTTP_BASIC_HEADER }, :body =>{mobile: 18600112233}) do |async|
async.response_header.status.should == 201
async.response.should == { mobile: '18600112233' }.to_json
end
end
end
end
添加其他帖子示例:
require 'spec_helper'
describe V1::API do
it "login" do
with_api V1::Login do
post_request(path: "/v1/login", :head => {'authorization' => HTTP_BASIC_HEADER }, :body =>{mobile: 18600112233}) do |async|
async.response_header.status.should == 201
async.response.should == { mobile: '18600112233' }.to_json
end
end
end
end
我对歌利亚一无所知。但如果你使用API类而不是APP类的话,它应该会起作用。我对歌利亚一无所知。但如果您使用API类而不是应用程序,它似乎应该可以工作