Javascript 茉莉花试验

Javascript 茉莉花试验,javascript,angularjs,unit-testing,jasmine,karma-jasmine,Javascript,Angularjs,Unit Testing,Jasmine,Karma Jasmine,我想用jasmine写一个测试。这是一个angular应用程序,我正在使用$window打开一个窗口。打开,然后设置几个事件侦听器 这是控制器: $scope.login = function() { LoadingService.show(); AccountsService.setPro($scope.pro); if ($scope.pro) { options.scope.push("repo"); } var githubU

我想用jasmine写一个测试。这是一个angular应用程序,我正在使用
$window打开一个窗口。打开
,然后设置几个事件侦听器

这是控制器:

$scope.login = function() {

    LoadingService.show();
    AccountsService.setPro($scope.pro);

    if ($scope.pro) {
        options.scope.push("repo");
    }

    var githubUrl = 'https://github.com/login/oauth/authorize?';
    var authUrl = githubUrl + 'client_id=' + options.client_id + '&scope=' + options.scope;
    var authWindow = $window.open(authUrl, '_blank', 'location=no,toolbar=yes,toolbarposition=top,closebuttoncaption=Close,clearcache=yes');

    authWindow.addEventListener('loadstart', function(e) {

        var url = (typeof e.url !== 'undefined' ? e.url : e.originalEvent.url),
            raw_code = /code=([^&]*)/.exec(e.url) || null,
            code = (raw_code && raw_code.length > 1) ? raw_code[1] : null,
            error = /\?error=(.+)$/.exec(e.url);

        if (code || error) {
            authWindow.close();
        }

        // If there is a code, proceed to get token from GitHub
        if (code) {
            requestToken(code);
        } else if (error) {
            AlertService.raiseAlert("Oops! Something went wrong and we couldn't log you in using Github. Please try again.");
            LoadingService.hide();
        }

    });

    // If "Done" button is pressed, hide "Loading"
    authWindow.addEventListener('exit', function(e) {
        LoadingService.hide();
    }, false);

};
这是我编写的测试,因为它无法进入
authWindow.addEventListener('loadstart',function(e){…}

it("Should go to the welcome screen.", function () {

    spyOn(loadingService, 'show');
    spyOn(accountsService, 'setPro');

    spy = jasmine.createSpy('message');

    window.addEventListener('message', function (e) {
        console.log(Object.keys(e), e.data);
        spy();
    });

    expect(scope.pro).toBeFalsy();

    var controller = createController();

    scope.togglePro();
    expect(scope.pro).toBeTruthy();

    scope.login();
    expect(loadingService.show).toHaveBeenCalled();

    window.postMessage('loadstart', 'http://www.github.com/?code="12313123');
    // window.postMessage('exit', '*');

    expect(accountsService.setPro).toHaveBeenCalledWith(scope.pro);

});
你知道我该怎么叫它事件监听器吗

// in your test add a mock for window (remember to reset back to normal window after)
windowmock.open = function(url, target, settings){

    //test the url, target and settings

    return {
        addEventListener: function(event, callback){
            if (event == 'loadstart'){
                callback({
                    url: 'something.com or whatever you're expecting,
                    originalEvent:{},

                });
            }
        },
        close: function(){}
    }
};