Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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 用sinon中断yield*函数调用_Javascript_Unit Testing_Mocha.js_Sinon_Yield - Fatal编程技术网

Javascript 用sinon中断yield*函数调用

Javascript 用sinon中断yield*函数调用,javascript,unit-testing,mocha.js,sinon,yield,Javascript,Unit Testing,Mocha.js,Sinon,Yield,我不熟悉函数*/收益/收益*以及单元测试,我不知道为什么我的单元测试代码不工作。它将sinon stubbing与mocha测试框架结合使用。我已经读过函数*/收益率/收益率*,但它仍然让我感到困惑 使用Co库,我有一个函数*,里面有一个yield*,它调用另一个函数*。我试图用sinon存根模拟yield*调用的函数*,但存根返回未定义。如果存根只是yield而不是yield*,则存根返回正确的响应 进口: import * as name from './file'; 调用原始生成器函数:

我不熟悉
函数*
/
收益
/
收益*
以及单元测试,我不知道为什么我的单元测试代码不工作。它将sinon stubbing与mocha测试框架结合使用。我已经读过
函数*
/
收益率
/
收益率*
,但它仍然让我感到困惑

使用Co库,我有一个
函数*
,里面有一个
yield*
,它调用另一个
函数*
。我试图用sinon存根模拟
yield*
调用的
函数*
,但存根返回未定义。如果存根只是
yield
而不是
yield*
,则存根返回正确的响应

进口:

import * as name from './file';
调用原始生成器函数:

export const func = (a, b, c) => co(secondFunc.bind(this, a, b, c));
函数secondFunc:

function* secondFunc(a, b, c) {
  try {
    const x = yield* name.get(a); // this is where x is undefined
    // logic
    return value;
  } catch (err) {
    // logic
  }
}
单元测试:

const callback = sinon.stub(name, 'get');
callback.returns(new Promise((resolved, reject) => resolved(response)));

co(func("a", "b", "c")).then((value) => {
    console.log(value);
    done();
}).catch(done);     

(请注意,原始代码不是我编写的。我只是添加了单元测试。)

这是与
yield*[expression]
一起使用的
co
模块的单元测试解决方案

file.ts

导出函数*get(a){
产生“真实数据”;
}
index.ts

import*作为“/file”中的名称;
从“co”进口co;
导出函数*secondFunc(a、b、c){
试一试{
const x=收益率*name.get(a);
返回x;
}捕捉(错误){
控制台错误(err);
}
}
导出常量func=(a,b,c)=>co(secondFunc.bind(null,a,b,c));
索引规范ts

从“/”导入{secondFunc,func};
从“chai”导入{expect};
将*作为名称从“/file”导入;
从“sinon”进口sinon;
从“co”进口co;
描述(“59308604”,()=>{
之后(()=>{
sinon.restore();
});
它(“应该通过secondFunc”,()=>{
函数*mGen(){
产生“假数据”;
}
const nameGetStub=sinon.stub(名称,“get”).returns(mGen());
const gen=secondFunc(“a”、“b”、“c”);
expect(gen.next().value).to.be.eql(“伪数据”);
expect(gen.next()).to.be.eql({value:undefined,done:true});
sinon.assert.calledWith(nameGetStub,“a”);
});
它(“应该传递func”,异步()=>{
函数*mGen(){
返回“假数据”;
}
const nameGetStub=sinon.stub(name,“get”).returns(mGen()如有);
常量实际=等待函数(“a”、“b”、“c”);
期望(实际)相等(“假数据”);
sinon.assert.calledWith(nameGetStub,“a”);
});
it(“测试公司”,()=>{
函数*g1(){
返回“123”;
}
返回co(函数*(){
var结果=收益率*g1();
返回结果;
})。然后((值)=>{
expect(value).to.be.eql(“123”);
});
});
});
单元测试结果和覆盖率报告:

59308604
✓ 应该通过secondFunc
✓ 应该通过func
✓ 测试公司
3次通过(17ms)
---------------|----------|----------|----------|----------|-------------------|
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s|
---------------|----------|----------|----------|----------|-------------------|
所有文件| 95.12 | 66.67 | 92.31 | 94.44 ||
file.ts | 50 | 0 | 0 | 50 | 2|
索引规范ts | 100 | 100 | 100 | 100 ||
指数:88.89 | 100 | 100 | 85.71 | 9|
---------------|----------|----------|----------|----------|-------------------|
源代码: