如何测试Sinatra Server-Sent Events应用程序?

时间:2018-04-02 20:22:52

标签: unit-testing sinatra server-sent-events

我正在构建一个小型聊天应用程序,以了解它如何与Sinatra一起工作。它应该是一个非常简单的应用程序,仅用于发送和接收消息。

这是我到目前为止所做的:     要求' sinatra'     要求' json'

class App < Sinatra::Base
  enable :logging
  set :server, :thin
  set :public_folder, File.dirname(__FILE__) + '/assets'

  connections = []

  get '/chat' do
    erb :chat
  end

  get '/stream' do
    content_type "text/event-stream"
    stream(:keep_open) do |out|
      connections << out
      connections.reject!(&:closed?)
    end
  end

  post '/:message' do
    connections.each do |out|
      out << params['message'] << "\n"
      out.close
    end

    "message received"
  end
end

require 'spec_helper'
require_relative '../app'
require 'pry'

RSpec.describe 'ChatServer' do
  def app
    App
  end

  it 'asserts true' do
    expect(true).to eq(true)
  end

  it 'posts message' do
    post '/message'
    expect(last_response.body).to eq('message received')
    expect(last_response.status).to eq(200)
  end

  it 'gets stream with correct content type' do
    get '/stream'
    expect(last_response.headers['Content-Type']).to include('event-stream')
  end

end

我的问题是如何对

等单元测试场景进行单元测试
  • 客户订阅get /stream
  • 发布/message发送消息&#39;至 订户

提前致谢!

0 个答案:

没有答案