这是我的功能
$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 是否存在!
首先,您的期望 (window.open).toHaveBeenCalled() 位置错误。
在监视事件之前你无法预期。
现在回答你的问题
茉莉花中有不同的方法来监视依赖关系,例如
-
.and.callThrough- 通过将间谍与 and.callThrough 链接起来,间谍仍将跟踪对其的所有调用,但此外它将委托给实际实现。
-
.and.callFake- 通过将间谍与 and.callFake 链接起来,对间谍的所有调用都将委托给提供的函数。
-
.and.returnValue- 通过将间谍与 and.returnValue 链接起来,对该函数的所有调用都将返回特定值。
请检查杰明医生 http://jasmine.github.io/2.0/introduction.html完整列表
根据您的要求提供以下示例测试用例
$scope.buildForm = function() {
$window.open( "http://www.google.com" );
};
Will be
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" );
} ) );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)