1. // backend test
  2.  
  3. beforeEach(inject(function (_$compile_, _$httpBackend_, _$rootScope_, _$state_, _AnnouncementsService_, _CONFIG_) {
  4. compile = _$compile_;
  5. $httpBackend = _$httpBackend_;
  6. $scope = _$rootScope_.$new();
  7.  
  8. AnnouncementsService = _AnnouncementsService_;
  9. CONFIG = _CONFIG_;
  10.  
  11. // Need to mock $state, so the ui-router resolve wont conflict with tests.
  12. state = _$state_;
  13. spyOn( state, 'go');
  14. spyOn( state, 'transitionTo');
  15.  
  16. directiveElem = getCompiledElement();
  17. directiveCtrl = directiveElem.controller('comAnnouncements');
  18. }));
  19.  
  20. afterEach(function() {
  21. $httpBackend.verifyNoOutstandingExpectation();
  22. $httpBackend.verifyNoOutstandingRequest();
  23. });
  24.  
  25. it('should respond 200 to a http get request and get 1 announcement', function(){
  26. AnnouncementsService.getAnnouncementData(2);
  27.  
  28. let expectedResponse = [
  29. {
  30. "id": 0,
  31. "title": "Maintenance work may affect Internet services 20/09/2015",
  32. "date": "11 Nov 2015 | 12:30 PM"
  33. }
  34. ];
  35.  
  36. $httpBackend.expectGET(CONFIG.BACKEND_API_URL + '/announcements/2').respond(200, { 'announcements': expectedResponse });
  37. $httpBackend.flush();
  38. expect(AnnouncementsService.announcementData).toEqual(expectedResponse);
  39. });
  40.  
  41. // ui-router test
  42.  
  43. it('Should move to "home state"', () => {
  44. $scope.$apply(() => {
  45. angular.element(directiveElem.find('a')[0]).click();
  46. $timeout.flush();
  47. });
  48.  
  49. expect($state.current.name).toEqual('selfcare.home');
  50. });
  51.  
  52. it('$state href should equal "/home"', () => {
  53. $scope.$apply(() => {
  54. $state.go('selfcare.home');
  55. });
  56. expect($state.current.name).toEqual('selfcare.home');
  57. expect($state.href('selfcare.home')).toEqual('/home');
  58. });
  59.  
  60. // Click to expect function to be called
  61.  
  62. it('should call goToSearch()', function(){
  63. spyOn(directiveCtrl, 'goToSearch');
  64. angular.element(directiveElem.find('.header-icon-menu i')[0]).click();
  65. expect(directiveCtrl.goToSearch).toHaveBeenCalled();
  66. });

[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. 学习日记_SSH框架web.xml配置文件篇

    1.参考一:http://www.blogjava.net/yxhxj2006/archive/2012/07/09/382632.html 2.参考二: <!-- web 容器启动spring ...

  2. LXPanel自定义添加应用程序到快速启动栏

    LXPanel是Linux下LXDE项目的一个桌面面板软件.我一开始接触的时候,对于自己自定义的程序到快速启动栏绕了很多弯路,这里记录下,防止以后自己忘了.还有一点就是很多时候,panel下的应用程序 ...

  3. 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 ...

  4. python学习第十八天 --文件操作

    这一章节主要讲解文件操作及其文件读取,缓存,文件指针. 文件操作 (1)文件打开:open(filepath,filemode) filepath:要打开文件的路径 filemode:文件打开的方式 ...

  5. debian小巧好看的桌面

    先看完,不然,你一定会后悔的..不好看,你打我.. sudo apt-get install xfce4 sudo apt-get install xfce4-goodies sudo apt-get ...

  6. 周赛C题 LightOJ 1047 (DP)

    C - C Time Limit:500MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu   Description Th ...

  7. tornado项目

    tornado项目之基于领域驱动模型架构设计的京东用户管理后台 本博文将一步步揭秘京东等大型网站的领域驱动模型,致力于让读者完全掌握这种网络架构中的“高富帅”. 一.预备知识: 1.接口: pytho ...

  8. 10个Java面试题及答案

    1. 什么是JVM? 为什么称Java为跨平台的编程语言? Java虚拟机(Java Virtual Machine)是可以执行Java字节码的虚拟机,每个Java源文件将被编译成字节码文件,然后在J ...

  9. Scut:从PackageReader分析客户端协议规则

    看第一个解析API: private void ParseData(byte[] data) { var paramBytes = SplitBuffer(data); RawParam = _enc ...

  10. iOS使用VLC

    简       注册登录 添加关注 作者 牵线小丑2016.03.18 10:42 写了4836字,被38人关注,获得了43个喜欢 iOS使用VLC 字数946 阅读698 评论1 喜欢14 简介 库 ...