小编Ste*_*fan的帖子

如何测试在异步测试中返回promise的存根?

如何以异步方式测试?

it('Should test something.', function (done) {

    var req = someRequest,
        mock = sinon.mock(response),
        stub = sinon.stub(someObject, 'method');

     // returns a promise
     stub.withArgs('foo').returns(Q.resolve(5));

     mock.expects('bar').once().withArgs(200);

     request(req, response);

     mock.verify();

});
Run Code Online (Sandbox Code Playgroud)

这是测试的方法.

var request = function (req, response) {

    ...

    someObject.method(someParameter)
        .then(function () {
            res.send(200);
        })
        .fail(function () {
            res.send(500);
        });

};
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我正在使用node.js,Q(用于承诺),sinon用于模拟和存根以及mocha作为测试环境.上面的测试由于请求方法的异步行为而失败,我不知道何时在测试中调用done().

javascript testing mocha.js promise sinon

9
推荐指数
1
解决办法
2万
查看次数

Node.js 和 Websocket 在 url 路径中带有通配符

是否有一个用于node.js的websocket框架,我可以在其中指定websockets服务器路径中的通配符?

我想使用这样的路径

/一些/:id

在这种情况下,应该能够连接到与上述字符串匹配的 url。服务器可以访问id并可以为不同的频道提供服务。

例如:当使用模块“ws”时,可以为 http 服务器上的一个特定路径设置 websocket 服务器:

new WebSocket.Server({server: someHttpServer, path:'/some/path'})
Run Code Online (Sandbox Code Playgroud)

从这里开始,您可以使用 WebSocket 对象从浏览器连接到您的 websocket 服务器:

let client = new WebSocket('ws://.../some/path')
Run Code Online (Sandbox Code Playgroud)

我正在寻找的是这样的东西

new WebSocket.Server({server: someHttpServer, path:'/some/:id'})
Run Code Online (Sandbox Code Playgroud)

这样我就可以连接到 websocket 并传递预定义的 id

let clientA = new WebSocket('ws://.../some/idA')
let clientB = new WebSocket('ws://.../some/idB')
Run Code Online (Sandbox Code Playgroud)

javascript websocket node.js

4
推荐指数
1
解决办法
2694
查看次数

JavaScript监听器不断增加

我实现了一个Web应用程序,并使用谷歌开发人员工具监控性能.我注意到听众不断增加,堆也是如此.

在此输入图像描述

听众增加的部分看起来像这样

let ival = $interval(function () {
    $http.get('someurl') // this call is actually done through a service, don't know if that matters
}, 1000)
Run Code Online (Sandbox Code Playgroud)

我会理解,如果堆增长是因为一些数据没有被垃圾收集器收集,但我不明白为什么听众会增加?

这是一个可重复的,最小的例子:

index.html文件:

<!doctype html>
<html ng-app="exampleModule">
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
    <script src="script.js"></script>
  </head>
  <body>
    <div ng-controller="someController">
    </div>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

和script.js文件:

angular.module("exampleModule", [])
  .controller("someController", [ "$http", "$interval", function ($http, $interval) {

    $interval(function () {
      $http.get('script.js')
    }, 1000)

  }])
Run Code Online (Sandbox Code Playgroud)

观看演奏时的结果与上图中的相同.您应该使用简单的Web服务器来发出GET请求.

javascript google-chrome listeners angularjs

1
推荐指数
1
解决办法
618
查看次数