Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 与摩卡柴和西农的测试承诺_Javascript_Promise_Mocha.js_Sinon_Sinon Chai - Fatal编程技术网

Javascript 与摩卡柴和西农的测试承诺

Javascript 与摩卡柴和西农的测试承诺,javascript,promise,mocha.js,sinon,sinon-chai,Javascript,Promise,Mocha.js,Sinon,Sinon Chai,我想使用mocha、chai和sinon测试我的承诺解析处理程序和承诺拒绝处理程序。此外,我已经设置了sinon chai插件和sinon存根承诺插件 这是我的require语句块: var chai = require('chai'); var expect = chai.expect; var sinonChai = require('sinon-chai'); chai.use(sinonChai); var sinon = require('sinon'); var sinonStubP

我想使用
mocha
chai
sinon
测试我的承诺解析处理程序和承诺拒绝处理程序。此外,我已经设置了
sinon chai
插件和
sinon存根承诺
插件

这是我的require语句块:

var chai = require('chai');
var expect = chai.expect;
var sinonChai = require('sinon-chai');
chai.use(sinonChai);
var sinon = require('sinon');
var sinonStubPromise = require('sinon-stub-promise');
sinonStubPromise(sinon);
这是我的测试套件:

describe('Connect to github users',function(done){

    var api = require('../users'),
        onSuccess = api.onSuccess,
        onError = api.onReject; 
    console.dir(api);
    //the idea is not to test the async connection,the idea is to test 
    //async connection but to test how the results are handled.
    var resolveHandler,
        rejectHandler,
        getPromise,
        result;

    beforeEach(function(){
        resolveHandler = sinon.spy(onSuccess);
        rejectHandler = sinon.spy(onError);
        getPromise = sinon.stub().returnsPromise();
    });

    it('must obtain the result when promise is successful',function(){
        result = [...];//is an actual JSON array    
        getPromise.resolves(result);

        getPromise()
            .then(resolveHandler)
            .catch(rejectHandler);

        expect(resolveHandler).to.have.been.called();//error 
        expect(resolveHandler).to.have.returned(result);
        expect(rejectHandler).to.have.not.been.called();    
        done();
    });

    afterEach(function(){
        resolveHandler.reset();
        rejectHandler.reset();
        getPromise.restore();
    });

});
我发现自己犯了这样的错误:

 Connect to github users must obtain the result when promise is successful:
 TypeError: expect(...).to.have.been.called.toEqual is not a function
  at Context.<anonymous> (C:\Users\vamsi\Do\testing_promises\test\githubUsersSpec.js:94:46)
Connect to github承诺成功时,用户必须获得结果:
TypeError:expect(…).to.have.been.called.toEqual不是函数
在上下文中。(C:\Users\vamsi\Do\testing\u promises\test\githubUsersSpec.js:94:46)

这里的这行代码是错误的:

expect(resolveHandler).to.have.been.called();
调用
只是一个属性,其值始终为
布尔值
,可以使用
chai
这样简单地进行测试:

expect(resolveHandler.called).to.equal(true);
类似地,代替此行以确认函数未被拒绝:

expect(rejectHandler).to.have.not.been.called();
将其与名为的
一起用作属性:

expect(rejectHandler.called).to.equal(false);

这里的这行代码是错误的:

expect(resolveHandler).to.have.been.called();
调用
只是一个属性,其值始终为
布尔值
,可以使用
chai
这样简单地进行测试:

expect(resolveHandler.called).to.equal(true);
类似地,代替此行以确认函数未被拒绝:

expect(rejectHandler).to.have.not.been.called();
将其与名为
一起用作属性:

expect(rejectHandler.called).to.equal(false);

这个包裹应该适合你所要做的事情。我遇到了同样的问题(除了我不需要测试拒绝案例),结果很好。

该软件包应该适合您尝试做的事情。我遇到了同样的问题(除了我不需要测试拒绝案例),结果很好。

存根承诺是同步的吗?实现是一个
表格
,它可以链接到任何有
的东西,然后
包括我不想做的真正承诺,因此,我使用
resolves
rejects
仅测试处理程序。在看了他在哪里捕获jQueryAjax成功回调的参数后,我尝试了这个方法,是的,我只是想知道,您希望在连接处理程序之后立即调用它们。正常的承诺会失败。存根承诺是同步的吗?实现是一个
,它可以与任何具有
然后
的东西链接,包括我不想做的真正承诺,因此我只使用
解析
拒绝
来测试处理程序。在看了他在哪里捕获jQueryAjax成功回调的参数后,我尝试了这个方法,是的,我只是想知道,您希望在连接处理程序之后立即调用它们。正常的承诺会失败。代码没有错,他试图使用
sinon chai
,因此
to.have.been.called
是有效代码。代码没有错,他试图使用
sinon chai
,因此
to.have.been.called
是有效代码。