angularjs - how to test $window.open using jasmine -
this function
$scope.buildform = function (majorobjectid, name) { $window.open("/formbuilder/index#/" + $scope.currentappid + "/form/" + majorobjectid + "/" + name); };
this jasmine test spec
it('should open new window buildform , 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); });
but when try run opening new tab , don't want happen, want check whether given returnvalues present not!!
first of expectation (window.open).tohavebeencalled() in wrong place. cannot expect before spying event. coming question there different methods in jasmine spy on dependencies,like
- .and.callthrough - chaining spy and.callthrough, spy still track calls in addition delegate actual implementation.
- .and.callfake - chaining spy and.callfake, calls spy delegate supplied function.
- .and.returnvalue - chaining spy and.returnvalue, calls function return specific value.
please check jamine doc complete list
sample test case below per requirement
$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" ); } ) );
Comments
Post a Comment