[Unit Testing] Angular Unit Testing, ui-router, httpbackend and spy
// 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的更多相关文章
- angular : $location & $state(UI router)的关系
次序:angular 的 location会先跑 $rootScope.$on("$locationChangeStart", function (scope, newUrl, o ...
- 【原创】ui.router源码解析
Angular系列文章之angular路由 路由(route),几乎所有的MVC(VM)框架都应该具有的特性,因为它是前端构建单页面应用(SPA)必不可少的组成部分. 那么,对于angular而言,它 ...
- angularjs ngRoute和ui.router对比
ngRoute模块是angularjs自带的路由模块,ui.router是一个第三方路由模块,接下来将对两者进行一个对比: ng-router(angular-router.js) ng-view n ...
- ngRoute 与ui.router区别
angular路由 路由 (route) ,几乎所有的 MVC(VM) 框架都应该具有的特性,因为它是前端构建单页面应用 (SPA) 必不可少的组成部分. 那么,对于 angular 而言,它自然也有 ...
- ngRoute 和 ui.router 的使用方法和区别
在单页面应用中要把各个分散的视图给组织起来是通过路由机制来实现的.本文主要对 AngularJS 原生的 ngRoute 路由模块和第三方路由模块 ui.router 的用法进行简单介绍,并做一个对比 ...
- [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 ...
- [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 ...
- [Angular & Unit Testing] Automatic change detection
When you testing Component rendering, you often needs to call: fixture.detectChanges(); For example: ...
- [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 ...
随机推荐
- iOS学习笔记(十四)——打电话、发短信
电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话 [[UIApplication sharedApplicatio ...
- 多核CPU利用测试
一直在想程序上是否特意让线程在指定的CPU上去运行,这样可以提高运行效率,所以特地写个代码让CPU使用率画正弦曲线的实验,我使用的是AMD X4 641的CPU,为四核四线程的片子. 代码如下 # ...
- hive 桶相关特性分析
1. hive 桶相关概念 桶(bucket)是指将表或分区中指定列的值为key进行hash,hash到指定的桶中,这样可以支持高效采样工作. 抽样( sampling )可以在全体数 ...
- Nginx配置文件nginx.conf详细说明
Nginx配置文件nginx.conf详细说明 #worker_processes 8; #worker_cpu_affinity 00000001 00000010 00000100 0000100 ...
- Java中的ExceptionInInitializerError异常及解决方法
当在静态初始化块中出现了异常的时候,JVM会抛出 java.lang.ExceptionInInitializerError异常.如果你了解Java中的静态变量,你会知道它们是在类加载的时候进行初始化 ...
- Spring Cp30配置
1.配置db.properties <bean id= "propertyConfigurer" class="org.springframework.beans. ...
- 网易DBA私享会分享会笔记2
mysql索引与查询优化什么是索引?索引其实是一个目录.通过各种数据结构实现,是(值=>行位置)的映射 索引的作用:1.提高访问速度2.实现主键.唯一键逻辑 索引使用场景数据量特别大的时候,进行 ...
- 自定义jdbc框架
获取元数据 元数据- DataBaseMetaData DatabaseMetaData meta = con.getMetaData(); getDatabaseProductName().getD ...
- HDU-1799(组合递推公式)
HDOJ-1799 - Fighting_Dream M - 暴力求解.打表 Time Limit:1000MS Memory Limit:32768KB 64bit IO Forma ...
- Mahout
http://blog.csdn.net/yueyedeai/article/details/26567379