// 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. Android-SVN

    服务器启动svn服务 svnserve -d -r /home/wbp/svn/actia/ 1 .svn  重新定位location , 改变新仓库的uuid , 今天操作SVN Client 发现 ...

  2. UIViewController的View显示在导航栏下面如何解决?

    ios7之前的版本中UIViewController中的view在显示后会自动调整为去掉导航栏的高度的,控件会自动在导航栏以下摆放. 在iOS7中UIViewController的wantsFullS ...

  3. Ubuntu下管理员界面的切换

    不同于redhat的Linux系统可以通过命令su root 简单地切换到管理员状态,ubuntn 默认是没有超级用户的,因此如需使用管理员权限可以使用以下2种方法: 1.sudo -i 此命令不需要 ...

  4. Specified VM install not found: type Standard VM, name jdk1.6.0_05

    重装系统换了jdk,之前jdk用的1.6,现在改成1.7了.但是更新之后发现ant打包用不了了,报错 Specified VM install not found: type Standard VM, ...

  5. 【HAOI2011】向量

    [题目描述] 给你一对数a,b,你可以任意使用(a,b), (a,-b), (-a,b), (-a,-b), (b,a), (b,-a), (-b,a), (-b,-a)这些向量,问你能不能拼出另一个 ...

  6. Oracle的卸载与安装

    今天在做一个CURD的web小应用,为后面使用ExtJS搭建一个后台.因为还没有使用过Oracle数据库,因此今天也特的地的使用oracle数据库作为后台的数据库,也当练习使用oracle. 但是今天 ...

  7. Jquery学习笔记1-jquery总体代码框架

    第一次在博客中记录自己的笔记,希望能坚持下去吧,加油! 今天学习的是Jquery的源代码,官网上下载,然后使用DW(dream waver)编辑器打开Js(下载的是未压缩版),版本是2.0.3.第一次 ...

  8. Sqlserver 正则替换函数的一种实现

    --函数 IF OBJECT_ID(N'dbo.RegexReplace') IS NOT NULL DROP FUNCTION dbo.RegexReplace GO CREATE FUNCTION ...

  9. connect函数

    TCP客户用connect函数来建立与TCP服务器的连接 int connect (int sockfd, const sockaddr * servaddr, socklen_t addrlen); ...

  10. java四种创建对象的方法

    1.用new语句创建对象,这是最常见的创建对象的方法.   2.运用反射手段,调用java.lang.Class或者java.lang.reflect.Constructor类的newInstance ...