首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >配置机架测试以间接启动服务器

配置机架测试以间接启动服务器
EN

Stack Overflow用户
提问于 2016-08-18 20:04:35
回答 2查看 261关注 0票数 0

下面是我的机架应用程序:

代码语言:javascript
运行
复制
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使用它。以下是我的测试:

代码语言:javascript
运行
复制
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发出请求,因为那里的服务器已经启动了?

EN

Stack Overflow用户

发布于 2016-08-18 21:59:00

你的应用应该是类名,例如,而不是:

代码语言:javascript
运行
复制
def app
  MainAppLogic.new
end

你必须使用

代码语言:javascript
运行
复制
def app
  MainAppLogic
end

您不需要指定执行get的端口,因为rack应用程序在测试的上下文中运行;因此这应该是正确的方式:

代码语言:javascript
运行
复制
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,应该是这样的:

代码语言:javascript
运行
复制
class MainAppLogic < Sinatra::Base
  get '/' do
    'Hello world'
  end
end
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39017784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档