简体   繁体   English

TDD:Sinon 2.x并尝试测试使用异步的同步方法

[英]TDD: Sinon 2.x and trying to test a sync method that uses async

So I've run into another snag, which I'm fighting with... I have a method that is a sync call, and within this method it calls a promise, async, method. 所以我遇到了另一个难题,我正在与之抗争……我有一个方法是一个同步调用,在这个方法中它调用了一个promise,async,方法。

in my app I have the following: 在我的应用程序中,我有以下内容:

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;
  }
}

The test I've tried the following within mocha 我在摩卡咖啡中尝试了以下测试

...

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);
    });
...

No matter what, I get configMockStub to always get false, while I am getting the menuService.getById.calledWith(1).to.equal(true) to equal true. 无论如何,当我将menuService.getById.callWith(1).to.equal(true)设置为true时,configMockStub始终会为false。 The test above was an attempt to try and get 'time' to pass. 上面的测试是尝试让“时间”通过的尝试。 I've tried it without and have equally failed. 我已经尝试过,但同样失败了。 I'm really striking out on ideas on how to test this. 我真的很想出如何测试的想法。 Maybe I have the code wrong to reference a promise inside this method. 也许我在此方法中引用一个Promise的代码错误。

The only thing I can say I don't have any choice over the configureRouter method. 我只能说我对configureRouter方法没有任何选择。 Any guidance is appreciated. 任何指导表示赞赏。

Thanks! 谢谢! Kelly 凯莉

Short answer: 简短答案:

I recently discovered I was trying to make configureRouter method be a synchronous call (making it use async await keywords). 我最近发现我试图使configureRouter方法成为同步调用(使其使用async await关键字)。 What I found out was Aurelia does allow that method to be promised. 我发现Aurelia确实允许使用该方法。 Because of this, the test in question is no longer an issue. 因此,该测试不再是问题。

Longer answer: 更长的答案:

The other part of this is that I had a slew of babel issues lining up between babelling for mocha, and then babelling for wallaby.js. 另一部分是,我遇到了很多通货膨胀问题,包括摩卡游乐和wallaby.js游乐。 For some reason these two were not playing well together. 由于某种原因,这两个人的表现不佳。

in the test above, another thing was to also change the following: 在上面的测试中,另一件事是还要更改以下内容:

it('should update router config', function () {

to

it('should update router config', async function () {

I feel like there was another step, but at this time I cannot recall. 我觉得还有一步,但是现在我不记得了。 In either case, knowing that I could use a promise made my world much easier for Aurelia. 无论哪种情况,知道我都可以兑现诺言使奥雷利亚变得更加轻松。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM