2012-10-19 42 views
1

我想寫一些測試,同時訪問我的小sinatra應用程序。Sinatra測試會話和併發訪問

這裏的問題是,我使用會話(通過Rack :: Session :: Pool)。我無法弄清楚如何讓機架測試產生一個新的會話。在我的請求中注入會話數據時,我總是會有一個會話。所以我基本上一次只能有一個會話。

在我的測試我試過如下:

threads = [] 
2.times do |index| 
    threads << Thread.new do 
    get "/controller/something", {}, "rack.session" => {:id => "Thread#{index}"} 
    post "/do_action" 
    end 
end 
thrads.each{|t| t.join} 

是否有某種「瀏覽器層,在那裏我可以有多個實例」?

編輯:我很抱歉,我必須澄清:線程的例子只是一個瘋狂的猜測,得到一個新的會議。它沒有工作。所以我只想找一種方法在runnin(測試)服務器上打開多個會話。在開發模式下,我可以打開一個新的瀏覽器會話來實現這樣的事情。在測試模式下,我目前失去了。

+0

@leify什麼是你要能與麒麟.sorry正確的做,如果我錯了西納特拉上的WEBrick和使用WEBrick基本上是單一線程工作,所以我沒有看到這種事情發生糾正我,如果我錯了 – Viren

+0

這可能是對的,但它與問題無關。線程示例來自客戶端。即使服務器只有一個線程,它也應該能夠管理多個會話。 – leifg

回答

1

下面是一個使用MiniTest和Spec語法擴展的工作示例。

# using MiniTest::Spec extensions 
# http://bfts.rubyforge.org/minitest/MiniTest/Spec.html 

describe 'Fun with Sinatra and multiple sessions' do 
    include Rack::Test::Methods 

    def app 
    Sinatra::Application 
    end 

    it "does some stuff with multiple sessions" do 
    sess1 = Rack::Test::Session.new(Rack::MockSession.new(app)) 
    sess2 = Rack::Test::Session.new(Rack::MockSession.new(app)) 
    sess1.wont_equal sess2 

    sess1.get '/' # or whatever 
    sess1.last_response.must_equal :ok? 

    sess2.get '/' # or whatever 
    sess2.last_response.must_equal :ok? 
    end 

    it "this does the same thing" do 
    sess2 = Rack::Test::Session.new(Rack::MockSession.new(app)) 

    get '/' # or whatever 
    last_response.must_equal :ok? 

    sess2.get '/' # or whatever 
    sess2.last_response.must_equal :ok? 

    end 

end