dimanche 5 juin 2016

testing Rails 4.1 with minitest and rack-test

I am new to tests in general and I can't figure it out how to make standard Rails 4.1 tests work with rack-test in the same ActionController::TestCase class.

I need this because the use case needs to call an controller action first (that setup a global hash for user presence) and then do a rack request that is intercept by rack-hijack (I am using MessageBus, which uses rack-hijack) and queries the user presence.

Example:

test 'user1 should enter chat and pool his own presence' do

    user1 = User1.new(social_logins(:user11))
    #enter user
    SocialLogin.stubs(:get_and_validate).yields([user1.sl, 200, 'OK'])
    post :enter, user1.remote_params
    assert_response :success
    body = JSON.parse(@response.body)
    puts "response body: #{@response.body}"
    assert_equal user1.sl.id, body['you_id']
    assert_equal 'OK', body['message']

    #prep
    browser = Rack::Test::Session.new(Rack::MockSession.new(FAgora::Application))     # using Rack::Test without the Mixin => http://ift.tt/VwvYQS
    MessageBus.long_polling_enabled = false

    browser.post "/message-bus/#{user1.mbus_clid}",
         {  '/presence/user' => 0  },
         {  'HTTP_X_USER_ID' => "user_#{user1.sl.id}" }

    puts browser.last_response.body
    assert browser.last_response.ok?
    body = JSON.parse(browser.last_response.body).last
    assert_equal user1.sl.id,      body['data']['enter']['user_id']
    assert_equal user1.mbus_clid,  body['data']['enter']['client_id']
end

When the test above runs, it creates problem with every other following test, like if there were 2 copies of the Rails app loaded.

Have anyone setup minitest and rack-test working together?

Aucun commentaire:

Enregistrer un commentaire