2017-04-21 78 views
1

作为一名Angular(2)开发人员,我最近开始尝试使用Aurelia。真的很喜欢它.. 但我真的有一些困难,单元测试Aurelia的Event Aggregator。这是我目前所拥有的,但是它现在不会在我的控制器中触发事件。我现在做错了,一些帮助会很棒!Aurelia:单元测试事件聚合器

// app.js 

@inject(UserService, EventAggregator) 
export class App { 
    constructor(userService, eventAggregator) { 
     this.userService = userService; 
     this.eventAggregator = eventAggregator; 
     this.authorizedUser = null; 

     // get authorized user 
     this.getAuthorizedUser(); 

     // subscribe to events 
     this.eventAggregator.subscribe(EVENTS.USER_LOGGED_IN, data => { 
      this.authorizedUser = data; 
     }); 
    } 

    // calls userService and sets this.authorizedUser; 
    getAuthorizedUser() { 
     .... 
    } 
} 

而且我的规格目前看起来是这样的:

describe('app',() => { 
    let component, 
     createComponent, 
     eventAggregator = new EventAggregator(), 
     userService = new UserService(); 

    beforeEach(() => {  
     component = StageComponent.withResources('app').inView('<app></app>'); 
     component.bootstrap(aurelia => { 
      aurelia.use.standardConfiguration(); 
      aurelia.container.registerInstance(UserService, userService); 
      aurelia.container.registerInstance(EventAggregator, eventAggregator); 
     }); 

     createComponent = component.create(bootstrap); 
    }); 

    // this one is working for example.. 
    it('should get authorized user when token is set', (done) => { 
     const result = 'some value'; 

     spyOn(UserService, 'getToken').and.returnValue(true); 
     spyOn(userService, 'getAuthorizedUser').and.returnValue(Promise.resolve('some value')); 

     createComponent.then(() => { 
      const viewModel = component.viewModel; 
      expect(viewModel.authorizedUser).toEqual(result); 
      done(); 
     }); 
    }); 

    // this one is failing (depending on Event Aggregator) 
    it('should set authorized user when LOGGED_IN event is fired', (done) => { 
     spyOn(UserService, 'getToken').and.returnValue(false); 

     createComponent.then(() => { 
      const viewModel = component.viewModel; 
      expect(viewModel.authorizedUser).toEqual(null); 

      eventAggregator.publish(EVENTS.USER_LOGGED_IN, 'some value'); 
      expect(viewModel.authorizedUser).toEqual('some value'); 
      done(); 
     }); 
    }); 

    afterEach(() => { 
     component.dispose(); 
    }); 


}); 
+0

你检查了EventAggregator的实例吗?他们肯定是一样的吗?我想他们应该,因为你注册了它的一个实例。不过,你可以尝试'registerSingleton()',但我认为它应该没有区别。 –

+0

这个问题是因为你使用'新的EventAggregator'。你的类需要使用EventAggregator类的__same__实例。另外 - 我猜'EVENTS.USER_LOGGED_IN'是某种字符串? – Tom

+0

@thebluefox感谢您的回复,是的,它是一个从常量导入的字符串。还更新回答我的问题(对我来说)工作解决方案。谢谢你的时间! –

回答

0

所以一些试验和错误之后,我发现了如何单元测试上面的代码。它并不是真正遵循Aurelia的文档,但我对这种测试方式非常满意。希望这可能会帮助你们中的一些人。不知道这是正确的方式,但它适用于我。请评论你的想法..

describe('app',() => { 
    let sut, 
     userServiceMock, 
     eventAggregator; 

    beforeEach(() => { 
     userServiceMock = new UserServiceMock(); // use a mock for the original service 
     eventAggregator = new EventAggregator(); 
     sut = new App(userServiceMock, eventAggregator); 
    }); 

    describe('subscribing to events',() => { 

     it('should set authorized user when LOGGED_IN event is fired', done => { 
      const authUser = {someKey: 'someValue'}; 

      // expect initial values 
      expect(sut.authorizedUser).toEqual(null); 

      // publish an event  
      eventAggregator.publish(EVENTS.USER_LOGGED_IN, authUser); 
           //^this is just a string constant 

      // expect the values changes triggered by the event 
      expect(sut.authorizedUser).toEqual(authUser); 

      done(); 
     }); 
    }); 

    afterEach(() => { 
     // sut.dispose() doesn't work here, still need to figure this out 
    }); 

});