首页 文章

Aurelia:单元测试Event Aggregator

提问于
浏览
1

作为一名Angular(2)开发人员,我最近开始尝试使用Aurelia . 真的很喜欢顺便说一下..但是我真的有一些难点单元测试Aurelia的 Event Aggregator . 这就是我目前所拥有的,但它并没有做错,有些帮助会很棒!

// 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();
    });


});

1 回答

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

相关问题