//Backend TestBeforeeach (Inject (function(_$compile_, _$httpbackend_, _$rootscope_, _$state_, _announcementsservice_, _config_) {compile=_$compile_; $httpBackend=_$httpbackend_; $scope= _$rootscope_.$New(); Announcementsservice=_announcementsservice_; CONFIG=_config_; //need to mock $state, so the ui-router resolve wont conflict with tests.State =_$state_; Spyon (state,' Go '); Spyon (state,' Transitionto '); Directiveelem=getcompiledelement (); Directivectrl= Directiveelem.controller (' comannouncements '); })); Aftereach (function() {$httpBackend. Verifynooutstandingexpectation (); $httpBackend. Verifynooutstandingrequest (); }); It (' should respond to a HTTP GET request and get 1 announcement ',function() {Announcementsservice.getannouncementdata (2); Let Expectedresponse= [ { "id": 0, "title": "Maintenance work affect Internet services 20/09/2015", "Date": 2015 Nov | 12:30pm PM " } ]; $httpBackend. Expectget (CONFIG. Backend_api_url+ '/ANNOUNCEMENTS/2 '). Respond ($, {' announcements '): Expectedresponse}); $httpBackend. Flush (); Expect (Announcementsservice.announcementdata). Toequal (Expectedresponse); }); //ui-router TestIt (' Should move to ' home state ', () = ={$scope. $apply (()={angular.element (Directiveelem.find (' A ') [0]). Click (); $timeout. Flush (); }); Expect ($state. current.name). Toequal (' Selfcare.home '); }); It (' $state href should equal "/home" ', () ={$scope. $apply (()={$state. Go (' Selfcare.home '); }); Expect ($state. current.name). Toequal (' Selfcare.home '); Expect ($state. href (' Selfcare.home '). Toequal (' home ')); }); //Click to expect function to be calledIt (' Should call Gotosearch () ',function() {Spyon (Directivectrl,' Gotosearch '); Angular.element (Directiveelem.find ('. Header-icon-menu i ') [0]). Click (); Expect (Directivectrl.gotosearch). tohavebeencalled (); });
[Unit Testing] Angular Unit testing, Ui-router, Httpbackend and Spy