Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何使用jasmine测试$window.open_Angularjs_Jasmine_Karma Runner - Fatal编程技术网

Angularjs 如何使用jasmine测试$window.open

Angularjs 如何使用jasmine测试$window.open,angularjs,jasmine,karma-runner,Angularjs,Jasmine,Karma Runner,这是我的职责 $scope.buildForm = function (majorObjectId, name) { $window.open("/FormBuilder/Index#/" + $scope.currentAppId + "/form/" + majorObjectId + "/" + name); }; 这是我的茉莉花测试规范 it('should open new window for buildForm and with expe

这是我的职责

 $scope.buildForm = function (majorObjectId, name) {
      $window.open("/FormBuilder/Index#/" + $scope.currentAppId + "/form/" + majorObjectId + "/" + name);
  };
这是我的茉莉花测试规范

            it('should open new window for buildForm and with expected id', function () {
            scope.majorObjectId = mockObjectId;
            scope.currentAppId = mockApplicationId;
            var name = "DepartmentMajor";
            scope.buildForm(mockObjectId, name);
            scope.$digest();
            expect(window.open).toHaveBeenCalled();
            spyOn(window, 'open');
            spyOn(window, 'open').and.returnValue("/FormBuilder/Index#/" + scope.currentAppId + "/form/" + scope.majorObjectId + "/" + name);
        });

但是当我尝试运行它时,它正在打开一个新选项卡,我不希望发生这种情况,我只想检查给定的返回值是否存在

首先,您的期望(window.open)。tohavebeenCall()位于错误的位置。 在监视事件之前,你不能期望。 现在来问你的问题 jasmine中有不同的方法来监视依赖项,如

  • .and.callThrough-通过将spy与and.callThrough链接,spy仍将跟踪对它的所有调用,但除此之外,它将委托给实际实现
  • .and.callFake-通过将spy与and.callFake链接,所有对spy的调用都将委托给提供的函数
  • .and.returnValue-通过将spy与and.returnValue链接,对函数的所有调用都将返回特定值
请查看完整的列表

根据您的要求,下面的示例测试用例

$scope.buildForm = function() {
        $window.open( "http://www.google.com" );
    };
将是

it( 'should test window open event', inject( function( $window ) {
        spyOn( $window, 'open' ).and.callFake( function() {
            return true;
        } );
        scope.buildForm();
        expect( $window.open ).toHaveBeenCalled();
        expect( $window.open ).toHaveBeenCalledWith( "http://www.google.com" );
    } ) );

它应该是
$window
我相信您的测试,Inject
$window
我injected$window,但即使它在运行后也会打开一个新选项卡