简体   繁体   中英

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.

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. 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.

The only thing I can say I don't have any choice over the configureRouter method. 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). What I found out was Aurelia does allow that method to be promised. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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