Unit testing Aurelia:单元测试事件聚合器
作为一个有角度的开发者,我最近开始尝试Aurelia。顺便说一句,我真的很喜欢。。 但是我在单元测试Aurelia的Unit testing Aurelia:单元测试事件聚合器,unit-testing,aurelia,Unit Testing,Aurelia,作为一个有角度的开发者,我最近开始尝试Aurelia。顺便说一句,我真的很喜欢。。 但是我在单元测试Aurelia的事件聚合器时确实遇到了一些困难。这是我目前拥有的,但它现在不会在我的控制器中触发事件。我现在做错了,一些帮助会很好 // app.js @inject(UserService, EventAggregator) export class App { constructor(userService, eventAggregator) { this.userS
事件聚合器时确实遇到了一些困难。这是我目前拥有的,但它现在不会在我的控制器中触发事件。我现在做错了,一些帮助会很好
// 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();
});
});
description('app',()=>{
让我们来看看组件,
创建组件,
eventAggregator=新的eventAggregator(),
userService=newuserservice();
在每个(()=>{
component=StageComponent.withResources('app').inView('');
引导(aurelia=>{
aurelia.use.standardConfiguration();
aurelia.container.registerInstance(UserService,UserService);
aurelia.container.registerInstance(EventAggregator,EventAggregator);
});
createComponent=component.create(引导);
});
//例如,这一个正在工作。。
它('设置令牌时应获得授权用户',(完成)=>{
常量结果='some value';
spyOn(UserService,'getToken')。和.returnValue(true);
spyOn(userService,'getAuthorizedUser')。和.returnValue(Promise.resolve('some value'));
创建组件。然后(()=>{
const viewModel=component.viewModel;
expect(viewModel.authorizedUser)、toEqual(result);
完成();
});
});
//此项失败(取决于事件聚合器)
它('触发登录事件时应设置授权用户',(完成)=>{
spyOn(UserService,'getToken')。和.returnValue(false);
创建组件。然后(()=>{
const viewModel=component.viewModel;
expect(viewModel.authorizedUser).toEqual(null);
eventAggregator.publish(EVENTS.USER_LOGGED_IN,“some value”);
expect(viewModel.authorizedUser).toEqual('some value');
完成();
});
});
之后(()=>{
component.dispose();
});
});
因此,经过一些尝试和错误,我发现了如何对上述代码进行单元测试。它并没有真正遵循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
});
});
您是否检查了EventAggregator的实例?他们肯定是一样的吗?我想他们应该,因为你注册了一个实例。但是,您可以尝试registerSingleton()
,尽管我认为这应该没有什么区别。问题在于您正在使用新的事件聚合器。您的类需要使用EventAggregator类的相同实例。另外-我猜是事件。登录的用户是某种类型的字符串?@BlueFox感谢您的回复,是的,它是作为常量从导入的字符串。还更新了一个(对我来说)工作解决方案来回答我的问题。谢谢你的时间!