[Unit Testing] Angular Unit testing, Ui-router, Httpbackend and Spy

Source: Internet
Author: User
Tags angular unit testing

//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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.