// 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. Python sys.path.append

    python sys.path.append 对于模块和自己写的程序不在同一个目录下,可以把模块的路径通过sys.path.append(路径)添加到程序中. 在程序开头加上: import syss ...

  2. 用java写bp神经网络(一)

    根据前篇博文<神经网络之后向传播算法>,现在用java实现一个bp神经网络.矩阵运算采用jblas库,然后逐渐增加功能,支持并行计算,然后支持输入向量调整,最后支持L-BFGS学习算法. ...

  3. [转]C++基本功和 Design Pattern系列 ctor & dtor

    今天Aear讲的是class.ctor 也就是constructor, 和  class.dtor, destructor. 相信大家都知道constructor 和 destructor是做什么用的 ...

  4. undefined 和 null 的异同

    在javascript中,undefined和Null是两个比较特殊的值.但有时候在判断时就有点迷糊.依个人浅见,整理如下: 1.数据类型 众多周知,在javascript中存在五种基本类型,即und ...

  5. Spring AOP原理及拦截器

    原理 AOP(Aspect Oriented Programming),也就是面向方面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP将应用系统分为两部分,核心业务逻辑(Core bu ...

  6. 使用HTML+CSS,jQuery编写的简易计算器

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  7. (转载)让ie6也支持max-width,和max-height实现图片等比例缩放

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 多台web服务器之间共享session

    常见的几种方法如下: 1. 写客户端Cookie的方式 当用户登陆成功以后,把网站域名.用户名.密码.token.session有效时间全部采用cookie的形式写入到客户端的cookie里面,如果用 ...

  9. 转:ElasticSearch 简单入门

    原文来自于:http://www.oschina.net/translate/elasticsearch-getting-started?cmp 教程样例 我们将要部署一个非常简单的应用--在一个部门 ...

  10. .net反射详解

    前言 之所以要写这篇关于C#反射的随笔,起因有两个:   第一个是自己开发的网站需要用到   其次就是没看到这方面比较好的文章. 所以下定决心自己写一篇,废话不多说开始进入正题. 前期准备 在VS20 ...