Reputation: 559
This is my function
$scope.buildForm = function (majorObjectId, name) {
$window.open("/FormBuilder/Index#/" + $scope.currentAppId + "/form/" + majorObjectId + "/" + name);
};
This is my jasmine test spec
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);
});
but when i try to run this it is opening a new tab and i don't want this to happen, i just want to check whether the given returnValues are present are not!!
Upvotes: 12
Views: 19976
Reputation: 9227
First of all your expectation (window.open).toHaveBeenCalled() is in wrong place. You cannot expect before spying the event. Now coming to your question there are different methods in jasmine to spy on dependencies,like
Please check Jamine doc for complete list
Sample test case for below as per your 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" );
} ) );
Upvotes: 27