使用npm测试进行sails.socket测试

sco*_*ott 5 javascript sockets testing sails.js

我正在使用sails.js v0.11.0而我正在进行单元测试.我可以通过http请求测试普通控制器,但我不知道从哪里开始通过套接字请求测试相同的调用.如果你有一个很好的资源或使用套接字的样本测试将是太棒了.

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: 'existing.user@email.com',
          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: 'invalid@email.com',
          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: 'existing.user@email.com',
          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: 'existing.user@email.com',
                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: 'invalid@email.com',
                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: 'existing.user@email.com',
                password: 'invalid1235'
              })
              .expect(403)
              .end(function(err) {
                done(err);
            });

          });

        });

      });
    });

  });

});
Run Code Online (Sandbox Code Playgroud)

sco*_*ott 1

我不能对此表示赞扬,但如果您偶然发现了这个问题并正在寻找答案,那就是。

在引导程序中将 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/';
Run Code Online (Sandbox Code Playgroud)

然后叫他们像这样使用它们。

//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: 'existing.user@email.com', password: 'admin1234' }, function (data, jwres) {
  assert.equal(jwres.statusCode, 200);
  done();  
 });
});
Run Code Online (Sandbox Code Playgroud)