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!!
Jasmine is a very popular JavaScript behavior-driven development (In BDD, you write tests before writing actual code) framework for unit testing JavaScript applications. It provides utilities that can be used to run automated tests for both synchronous and asynchronous code.
Running a single specBy using fit (think of it as a focused it ), Jasmine will run only that particular spec. describe('Awesome feature', function () { fit('should check whether `true` is really `true`', function () { expect(true). toBe(true); }); });
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" );
} ) );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With