Unit testing 如何测试Sinatra服务器发送事件应用程序?
我正在构建一个小型聊天应用程序,以了解it流如何与Sinatra协同工作。它应该是一个非常简单的应用程序,只用于发送和接收消息 这就是我到目前为止所做的: 需要“sinatra” 需要“json”Unit testing 如何测试Sinatra服务器发送事件应用程序?,unit-testing,sinatra,server-sent-events,Unit Testing,Sinatra,Server Sent Events,我正在构建一个小型聊天应用程序,以了解it流如何与Sinatra协同工作。它应该是一个非常简单的应用程序,只用于发送和接收消息 这就是我到目前为止所做的: 需要“sinatra” 需要“json” class App < Sinatra::Base enable :logging set :server, :thin set :public_folder, File.dirname(__FILE__) + '/assets' connections = [] get
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
我的问题是如何进行单元测试场景
- 客户端在get
/stream
- post
将“消息”发送到 订户/message
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