[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 ...
随机推荐
- 学习日记_SSH框架web.xml配置文件篇
1.参考一:http://www.blogjava.net/yxhxj2006/archive/2012/07/09/382632.html 2.参考二: <!-- web 容器启动spring ...
- LXPanel自定义添加应用程序到快速启动栏
LXPanel是Linux下LXDE项目的一个桌面面板软件.我一开始接触的时候,对于自己自定义的程序到快速启动栏绕了很多弯路,这里记录下,防止以后自己忘了.还有一点就是很多时候,panel下的应用程序 ...
- CentOS6.5安装LAMP环境APACHE的安装
1.卸载apr.apr-util [root@centos6 LAMP]# yum remove apr apr-util 2.编译安装apr-1.5.1.tar.gz [root@centos6 L ...
- python学习第十八天 --文件操作
这一章节主要讲解文件操作及其文件读取,缓存,文件指针. 文件操作 (1)文件打开:open(filepath,filemode) filepath:要打开文件的路径 filemode:文件打开的方式 ...
- debian小巧好看的桌面
先看完,不然,你一定会后悔的..不好看,你打我.. sudo apt-get install xfce4 sudo apt-get install xfce4-goodies sudo apt-get ...
- 周赛C题 LightOJ 1047 (DP)
C - C Time Limit:500MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Description Th ...
- tornado项目
tornado项目之基于领域驱动模型架构设计的京东用户管理后台 本博文将一步步揭秘京东等大型网站的领域驱动模型,致力于让读者完全掌握这种网络架构中的“高富帅”. 一.预备知识: 1.接口: pytho ...
- 10个Java面试题及答案
1. 什么是JVM? 为什么称Java为跨平台的编程语言? Java虚拟机(Java Virtual Machine)是可以执行Java字节码的虚拟机,每个Java源文件将被编译成字节码文件,然后在J ...
- Scut:从PackageReader分析客户端协议规则
看第一个解析API: private void ParseData(byte[] data) { var paramBytes = SplitBuffer(data); RawParam = _enc ...
- iOS使用VLC
简 注册登录 添加关注 作者 牵线小丑2016.03.18 10:42 写了4836字,被38人关注,获得了43个喜欢 iOS使用VLC 字数946 阅读698 评论1 喜欢14 简介 库 ...