如何测试需要websocket的Sinatra方法?

时间:2013-06-25 16:06:35

标签: ruby websocket sinatra minitest

我刚刚开始使用Sinatra,并希望能够测试一个检查websocket的方法,如下面的代码片段所示:

require 'sinatra'
require 'sinatra-websocket'

set :server, 'thin'
set :sockets, []

get '/' do
    if !request.websocket?
        erb :index
    else
        request.websocket do |ws|
            ws.onopen do
                ws.send("Hello World!")
                settings.sockets << ws
            end
            ws.onmessage do |msg|
                EM.next_tick { settings.sockets.each{|s| s.send(msg) } }
            end
            ws.onclose do
                warn("websocket closed")
                settings.sockets.delete(ws)
            end
        end
    end
end

(代码从this question解除,但几乎与我的使用相同)

我正在使用MiniTest进行测试,希望有一种简单的方法可以使用模拟websocket传递请求,因此超越了if!request.websocket?有条件的。

使用最简单示例的片段会很棒,但任何指针都会感激不尽!

免责声明:我是Ruby的新手,虽然我喜欢它,虽然我没有看到任何明确的使用示例,但这看起来很明显 - 因此这个问题!

0 个答案:

没有答案
相关问题