[英]TDD: Sinon 2.x and trying to test a sync method that uses async
所以我遇到了另一个难题,我正在与之抗争……我有一个方法是一个同步调用,在这个方法中它调用了一个promise,async,方法。
在我的应用程序中,我有以下内容:
export class App {
constructor(menuService) {
_menuService = menuService;
this.message = "init";
}
configureRouter(config, router) {
console.log('calling configureRouter');
_menuService.getById(1).then(menuItem => {
console.log('within then');
console.log(`configureRouter ${JSON.stringify(menuItem, null, 2)}`);
const collection = menuItem.links.map(convertToRouteCollection);
console.log(`collection ${JSON.stringify(collection, null, 2)}`);
//I think there is an issue with asyn to synch for the test
config.map(collection);
}).catch(err => {
console.error(err);
});
console.log('calling configureRouter assign router');
this.router = router;
}
}
我在摩卡咖啡中尝试了以下测试
...
it('should update router config', function () {
const expectedData = {
name: "main menu",
links: [{
url: '/one/two',
name: 'link name',
title: 'link title'
}]
};
const configMapStub = sinon.stub();
const config = {
map: configMapStub
};
const routerMock = sinon.stub();
let app = null;
const actualRouter = null;
let menuService = null;
setTimeout(() => {
menuService = {
getById: sinon.stub().returns(Promise.resolve(expectedData).delay(1))
};
app = new App(menuService);
app.configureRouter(config, routerMock);
}, 10);
clock.tick(30);
expect(app.router).to.equal(routerMock);
expect(menuService.getById.calledWith(1)).to.equal(true);
//console.log(configMapStub.args);
expect(configMapStub.called).to.equal(true);
const linkItem = expectedData.links[0];
const actual = [{
route: ['', 'welcome'],
name: linkItem.name,
moduleId: linkItem.name,
nav: true,
title: linkItem.title
}];
console.log(`actual ${JSON.stringify(actual, null, 2)}`);
expect(config.map.calledWith(actual)).to.equal(true);
});
...
无论如何,当我将menuService.getById.callWith(1).to.equal(true)设置为true时,configMockStub始终会为false。 上面的测试是尝试让“时间”通过的尝试。 我已经尝试过,但同样失败了。 我真的很想出如何测试的想法。 也许我在此方法中引用一个Promise的代码错误。
我只能说我对configureRouter方法没有任何选择。 任何指导表示赞赏。
谢谢! 凯莉
简短答案:
我最近发现我试图使configureRouter方法成为同步调用(使其使用async await关键字)。 我发现Aurelia确实允许使用该方法。 因此,该测试不再是问题。
更长的答案:
另一部分是,我遇到了很多通货膨胀问题,包括摩卡游乐和wallaby.js游乐。 由于某种原因,这两个人的表现不佳。
在上面的测试中,另一件事是还要更改以下内容:
it('should update router config', function () {
至
it('should update router config', async function () {
我觉得还有一步,但是现在我不记得了。 无论哪种情况,知道我都可以兑现诺言使奥雷利亚变得更加轻松。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.