这是我的功能
$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);
});
但是当我尝试运行它时它会打开一个新选项卡,我不希望这种情况发生,我只想检查给定的returnValues是否存在!
答案 0 :(得分:18)
首先,您的期望(window.open).toHaveBeenCalled()位置错误。 在监视事件之前你不能指望。 现在回答你的问题 茉莉花有不同的方法可以监视依赖关系,比如
请检查Jamine doc以获取完整列表
根据您的要求提供下面的示例测试用例
$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" );
} ) );