// backend test

        beforeEach(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 200 to a http get request and get 1 announcement', function(){
AnnouncementsService.getAnnouncementData(2); let expectedResponse = [
{
"id": 0,
"title": "Maintenance work may affect Internet services 20/09/2015",
"date": "11 Nov 2015 | 12:30 PM"
}
]; $httpBackend.expectGET(CONFIG.BACKEND_API_URL + '/announcements/2').respond(200, { 'announcements': expectedResponse });
$httpBackend.flush();
expect(AnnouncementsService.announcementData).toEqual(expectedResponse);
}); // ui-router test it('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 called it('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的更多相关文章

  1. angular : $location & $state(UI router)的关系

    次序:angular 的 location会先跑 $rootScope.$on("$locationChangeStart", function (scope, newUrl, o ...

  2. 【原创】ui.router源码解析

    Angular系列文章之angular路由 路由(route),几乎所有的MVC(VM)框架都应该具有的特性,因为它是前端构建单页面应用(SPA)必不可少的组成部分. 那么,对于angular而言,它 ...

  3. angularjs ngRoute和ui.router对比

    ngRoute模块是angularjs自带的路由模块,ui.router是一个第三方路由模块,接下来将对两者进行一个对比: ng-router(angular-router.js) ng-view n ...

  4. ngRoute 与ui.router区别

    angular路由 路由 (route) ,几乎所有的 MVC(VM) 框架都应该具有的特性,因为它是前端构建单页面应用 (SPA) 必不可少的组成部分. 那么,对于 angular 而言,它自然也有 ...

  5. ngRoute 和 ui.router 的使用方法和区别

    在单页面应用中要把各个分散的视图给组织起来是通过路由机制来实现的.本文主要对 AngularJS 原生的 ngRoute 路由模块和第三方路由模块 ui.router 的用法进行简单介绍,并做一个对比 ...

  6. [Angular + Unit Testing] Mock HTTP Requests made with Angular’s HttpClient in Unit Tests

    In a proper unit test we want to isolate external dependencies as much as possible to guarantee a re ...

  7. [Angular & Unit Testing] Testing Component with Store

    When using Ngrx, we need to know how to test the component which has Router injected. Component: imp ...

  8. [Angular & Unit Testing] Automatic change detection

    When you testing Component rendering, you often needs to call: fixture.detectChanges(); For example: ...

  9. [Angular Unit Testing] Debug unit testing -- component rendering

    If sometime you want to log out the comonent html to see whether the html render correctly, you can ...

随机推荐

  1. 第十二章作业 MemoryBugs-master项目优化笔记

    作业要求: 下载bug项目:https://github.com/lzyzsd/MemoryBugs,请注意配合使用MemoryMonitor, AllocationTracker以及HeapDump ...

  2. 安装php时,make步骤报错make: *** [sapi/fpm/php-fpm] Error 1

    安装PHP过程中,make步骤报错:(集中网络上各种解决方法) (1)-liconv -o sapi/fpm/php-fpm /usr/bin/ld: cannot find -liconv coll ...

  3. 01-android快速入门

    adb Android debug bridge 安卓调试桥 创建模拟器,屏幕尽量小些,启动速度运行速度快 Android项目的目录结构 Activity:应用被打开时显示的界面 src:项目代码 R ...

  4. SWT/RAP计算器

    /** *2.测试 */ public class NofTest extends AbstractEntryPoint {        Text text,text2;    RemoteObje ...

  5. openssl 使用非阻塞 bio

    序 在项目中需要访问 https 加密的网页,为了保证并发性,需要用到非阻塞的 socket,搜索发现,这种使用场景的相关介绍不是很多,所以这里记录一下使用的过程. 在项目中,所使用的 ssl 库是老 ...

  6. SAS学习笔记

    一.            在SAS中进行随机抽样: 1. 在实际数据处理中常常需要进行样本抽样,在实践中主要有两种情况: (1)简单无重复抽样(2)分层抽样   a.等比例分层抽样  b. 不等比例 ...

  7. [jQuery编程挑战]002:实现一个转盘大抽奖

    body { background-color: #F2F2F2; text-align: center; } .container { position: relative; width: 500p ...

  8. [转]JavaScript函数和数组总结

    转自:http://www.uml.org.cn/AJAX/201307264.asp 写的不错,对我有很多帮助,最近准备全面的学习研究一下ES5,先转载一下这篇文章. JavaScript函数 1. ...

  9. gchart 插件API

    data: 一个二维数组,参数类型如下:[[, , ], [, , ], [, , ]] size: 图片显示的大小 ( width x height ) 300x200 type: 前面已经说过了 ...

  10. JS键盘的键码(event.keyCode)

    keycode 8 = BackSpace BackSpace keycode 9 = Tab Tab keycode 12 = Clear keycode 13 = Enter keycode 16 ...