下面是我的机架应用程序:
class MainAppLogic
def initialize
Rack::Server.start(:app =>Server, :server => "WEBrick", :Port => "8080")
end
end
class Server
def self.call(env)
return [200, {},["Hello, World"]]
end
end当实际运行时,它的行为是正常的,并向所有请求返回"Hello World“。我很难说服rack-test使用它。以下是我的测试:
require "rspec"
require "rack/test"
require "app"
# Rspec config source: https://github.com/shiroyasha/sinatra_rspec
RSpec.configure do |config|
config.include Rack::Test::Methods
end
describe MainAppLogic do
# App method source: https://github.com/shiroyasha/sinatra_rspec
def app
MainAppLogic.new
end
it "starts a server when initialized" do
get "/", {}, "SERVER_PORT" => "8080"
last_response.body.should be != nil
end
end当我测试它时,它失败了,它报告说MainAppLogic不是一个机架服务器,特别是它不响应MainAppLogic.call。我如何才能让它知道忽略MainAppLogic不是机架服务器,而只是向localhost:8080发出请求,因为那里的服务器已经启动了?
发布于 2016-08-18 21:59:00
你的应用应该是类名,例如,而不是:
def app
MainAppLogic.new
end你必须使用
def app
MainAppLogic
end您不需要指定执行get的端口,因为rack应用程序在测试的上下文中运行;因此这应该是正确的方式:
it "starts a server when initialized" do
get "/"
last_response.body.should be != nil
end此外,由于建议使用新的expect格式而不是should,请参见http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
和你的MainAppLogic,应该是这样的:
class MainAppLogic < Sinatra::Base
get '/' do
'Hello world'
end
endhttps://stackoverflow.com/questions/39017784
复制相似问题