我正在构建一个小型聊天应用程序,以了解它如何与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
我的问题是如何对
等单元测试场景进行单元测试/stream
/message
发送消息&#39;至
订户提前致谢!