【问题标题】:sails.socket testing with npm test使用 npm test 进行sails.socket 测试
【发布时间】:2015-06-30 20:41:12
【问题描述】:

我正在使用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);
            });

          });

        });

      });
    });

  });

});

【问题讨论】:

    标签: javascript sockets testing sails.js


    【解决方案1】:

    我发现 Sails 有我认为很好的关于这个主题的文档,但没有直接在他们的 normal Sails documentation 关于这个主题(尽管首先阅读它是很好的常识材料)。相反,我不得不阅读他们的github sails.io.js project。同样,比 README 更好的是他们的示例。查看this file 以查看他们用于测试套接字的设置和拆卸,以及展示测试本身的this file

    【讨论】:

    • 感谢您的回复!
    【解决方案2】:

    我不能为此声称功劳,但如果你偶然发现了这个问题并正在这里寻找答案。

    在引导程序中将 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: 'existing.user@email.com', password: 'admin1234' }, function (data, jwres) {
      assert.equal(jwres.statusCode, 200);
      done();  
     });
    });
    

    【讨论】:

    • 当我运行这个时,我得到:ReferenceError: navigator is not defined
    • 嗨斯科特,这是推荐的方式吗?还是泰勒提到的方式-stackoverflow.com/a/38819560/1828637
    • 它似乎没有为我使用 cookie。我已登录,但一直收到未经授权的回复。
    猜你喜欢
    • 2018-06-26
    • 1970-01-01
    • 2014-10-05
    • 2017-07-28
    • 2016-10-30
    • 2011-06-22
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    相关资源
    最近更新 更多