2015-04-22 40 views
3

我使用的是sails.js v0.11.0,我剛剛進入單元測試。我可以通過http請求測試正常的控制器,但我不知道從哪裏開始通過套接字請求測試相同的調用。如果你有一個很好的資源或使用套接字的樣本測試,那就太棒了。使用npm測試的sails.socket測試

var assert = require('assert'); 
var request = require('supertest'); 

describe('Auth Controller', function() { 

    describe('#callback()', function() { 

    it ('passport-local authentication should succeed if email and password valid', function (done) { 

     request(sails.hooks.http.app) 
     .post('/auth/local') 
     .send({ 
      identifier: '[email protected]', 
      password: 'admin1234' 
     }) 
     .expect(200) 
     .end(function(err) { 
      done(err); 
     }); 

    }); 

    it ('passport-local authentication should fail and return error code if email is invalid', function (done) { 

     request(sails.hooks.http.app) 
     .post('/auth/local') 
     .send({ 
      identifier: '[email protected]', 
      password: 'admin1234' 
     }) 
     .expect(403) 
     .end(function(err) { 
      done(err); 
     }); 

    }); 

    it ('passport-local authentication should fail and return error code if password is invalid', function (done) { 

     request(sails.hooks.http.app) 
     .post('/auth/local') 
     .send({ 
      identifier: '[email protected]', 
      password: 'invalid1235' 
     }) 
     .expect(403) 
     .end(function(err) { 
      done(err); 
     }); 

    }); 

    //Test with Web Sockets from sails.io 
    describe('sails.socket', function() { 

     describe('With default settings', function() { 

     describe('once connected, socket', function() { 

      it ('passport-local authentication via web socket should succeed if email and password valid', function (done) { 

      //Socket version? 
      request(sails.hooks.http.app) 
       .post('/auth/local') 
       .send({ 
       identifier: '[email protected]', 
       password: 'admin1234' 
       }) 
       .expect(200) 
       .end(function(err) { 
       done(err); 
       }); 

      }); 

      it ('passport-local authentication via web socket should fail and return error code if email is invalid', function (done) { 

      //Socket version? 
      request(sails.hooks.http.app) 
       .post('/auth/local') 
       .send({ 
       identifier: '[email protected]', 
       password: 'admin1234' 
       }) 
       .expect(403) 
       .end(function(err) { 
       done(err); 
       }); 

      }); 

      it ('passport-local authentication via web socket should fail and return error code if password is invalid', function (done) { 

      //Socket version? 
      request(sails.hooks.http.app) 
       .post('/auth/local') 
       .send({ 
       identifier: '[email protected]', 
       password: 'invalid1235' 
       }) 
       .expect(403) 
       .end(function(err) { 
       done(err); 
      }); 

      }); 

     }); 

     }); 
    }); 

    }); 

}); 

回答

0

我不能爲此承認功勞,但是請您絆倒這個問題,並且在這裏尋找答案。

申報IO作爲引導全球:

// test/boostrap.test.js 

var client = require('../assets/js/dependencies/sails.io.js'); 

global.io = new client(require('socket.io-client')); 
io.sails.url = 'http://localhost:1337/'; 

然後打電話給他們使用它們像這樣。

//test/callbacks.test.js 
describe('socket request', function() { 

it ('passport-local authentication should succeed if email and password valid', function (done) { 

    io.socket.post('/auth/local', { identifier: '[email protected]', password: 'admin1234' }, function (data, jwres) { 
    assert.equal(jwres.statusCode, 200); 
    done(); 
}); 
}); 
1

我發現,風帆具有什麼,我會考慮在這個問題上良好的文檔,但關於這個問題他們normal Sails documentation直接(雖然這是很好的一般知識材料已經閱讀第一)。相反,我不得不閱讀他們的github sails.io.js project。同樣,比自述更好的是他們的例子。查看this file以查看他們的測試套接字的設置和拆卸情況,以及this file,它們展示了測試本身。

+0

感謝您的迴應! – scott