Asked 8 years ago
3 Oct 2016
Views 1259
ajamil

ajamil posted

use of expect function in the Angular js

working with cordova App, and i jst started learning Angular js and checking some PasswordController . i found some code

it('sets the strength to "strong" if the password length is >8 chars', function() {
      var $scope = {};
      var controller = $controller('PasswordController', { $scope: $scope });
      $scope.password = 'longerthaneightchars';
      $scope.grade();
      expect($scope.strength).toEqual('strong');
    });


want to know what is use of expect in this scenario ?
Mahesh Radadiya

Mahesh Radadiya
answered Nov 30 '-1 00:00

expect is used to check with real value to expected value and based on that it give test - pass or fail status

the expect is the chained "Matcher" function suppose toEqual or toBe , so


    expect(true).toBe(true); 


so expect take real value(true) and "Matchers"(toBe) function will take expected value(true)
Matchers function implement Boolean comparison between real and expected value and it decide if test failed or passed


          expect(element(by.id('link-1')).getAttribute('href')).toBe('');

so in above example if element which have id "link-1" and its href no to be "" (empty) than test get failed result

Hope explanation helps to understand
Phpworker

Phpworker
answered Nov 30 '-1 00:00

mostly used for Unit Testing , you can see "expect " with some of the testing tool in angular js

1. Karma - Command line tool test tool for AngularJs
2. Jasmine- Behavior Driven Development Framework for Angular Js
3. Protractor - end-to-end test framework for AngularJS applications.

many other also use expect for Angular Js Testing
Post Answer