【问题标题】:creating socket.io server in protractor test在量角器测试中创建 socket.io 服务器
【发布时间】:2015-09-15 09:38:54
【问题描述】:

我正在尝试为连接到 socket.io 服务器的角度前端创建量角器测试。在我的测试中,我打开了一个新的 socket.io 服务器并在测试结束时将其关闭,但我发现旧测试的回调仍在运行,而不是新的。 我做错了什么?

我的简化量角器代码:

var Server = require('socket.io');

describe('test', function () {
    var io;

    beforeEach(function () {
        io = Server(3800);
        var test = jasmine.getEnv().currentSpec.description;
        io.on('connection', function () {
            var current = jasmine.getEnv().currentSpec.description;
            if (test !== current) {
                // This line shouldn't run
                console.error('Connection been made to server of test ' + test +
                    ' while current test is test ' + current);
            }
        });
    });

    it('one', function () {
        browser.get('http://localhost:8080');
        var label = element(By.binding('status'));
        expect(label.getText()).toBe('connected');
    });

    it('two', function () {
        browser.get('http://localhost:8080');
        var label = element(By.binding('status'));
        expect(label.getText()).toBe('connected');
    });

    afterEach(function () {
        io.close();
    })
});

还有我简化的 angular-app:

<html>
<head>
    <script src="angular.js"></script>
    <script src="socket.io.js"></script>
    <script>
        angular.module('test', [])
                .controller('MainCtrl', function ($scope) {
                    var socket = io('localhost:3800');
                    socket.on('connect', function () {
                        $scope.status = 'connected';
                        $scope.$digest();
                    })
                })
    </script>
</head>
<body ng-app="test">
    <div ng-controller="MainCtrl">
        {{ status }}
    </div>
</body>
</html>

【问题讨论】:

  • io.close() 可能是异步的,所以afterEachio 实际关闭之前完成(然后下一个可能无法启动,因为端口已经在使用中,等等,等等)。也许您可以教 Protractor 等待服务器完全完成(或者教 beforeEach 在遇到套接字使用错误时重试。)(尽管您应该首先验证 io.close() 确实是问题所在。)

标签: angularjs node.js socket.io protractor


【解决方案1】:

作为 P.T.提到您应该等待连接打开和关闭,然后再移动到下一个块。

describe('test', function () {


    beforeEach(function (done) { //wait for me
        var that = this;
        this.io = Server(3800);
        this.io.on('connection', function (socket) {
            that.activeSocket = socket;
            done(); //ready
        });
    });

    it('one', function () {
        browser.get('http://localhost:8080');
        var label = element(By.binding('status'));
        expect(label.getText()).toBe('connected');
    });

    it('two', function () {
        browser.get('http://localhost:8080');
        var label = element(By.binding('status'));
        expect(label.getText()).toBe('connected');
    });

    afterEach(function (done) {
        this.io.close();
        this.activeSocket.on('disconnect', function () {
          done();
        });

    })
});

【讨论】:

  • 嗨,我试过你的代码。但是在“beforeEach”函数中有一个超时,我通过将 browser.get 移动到“beforeEach”来解决这个问题。但我仍然会超时,因为 activeSocket 永远不会断开连接。
  • 仍然收到茉莉花超时。 (编辑您的代码并将浏览器移至 afterEach)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多