AngularJS Test

PS: 个人学习备忘,一开始看Angular的手册碰到这里看不懂了,囧

Developer
Guide

Free courses

啥也不说了, Angular太酷了,不愧是谷歌出品,必出精品!设计很完善,开发模式很好,可以让整个项目工程化!另外,测试支持也很完善,比如单元测试用 karma,端到端测试用 protractor

1. controller里面的最下面的那段 controller test

答案 

Controller Test:

describe('myController function', function() {

  describe('myController', function() {
    var $scope;

    beforeEach(module('myApp'));

    beforeEach(inject(function($rootScope, $controller) {
      $scope = $rootScope.$new();
      $controller('MyController', {$scope: $scope});
    }));

    it('should create "spices" model with 3 spices', function() {
      expect($scope.spices.length).toBe(3);
    });

    it('should set the default value of spice', function() {
      expect($scope.spice).toBe('habanero');
    });
  });
});

If you need to test a nested Controller you need to create the same scope hierarchy in your test that exists in the DOM:

describe('state', function() {
    var mainScope, childScope, grandChildScope;

    beforeEach(module('myApp'));

    beforeEach(inject(function($rootScope, $controller) {
        mainScope = $rootScope.$new();
        $controller('MainController', {$scope: mainScope});
        childScope = mainScope.$new();
        $controller('ChildController', {$scope: childScope});
        grandChildScope = childScope.$new();
        $controller('GrandChildController', {$scope: grandChildScope});
    }));

    it('should have over and selected', function() {
        expect(mainScope.timeOfDay).toBe('morning');
        expect(mainScope.name).toBe('Nikki');
        expect(childScope.timeOfDay).toBe('morning');
        expect(childScope.name).toBe('Mattie');
        expect(grandChildScope.timeOfDay).toBe('evening');
        expect(grandChildScope.name).toBe('Gingerbread Baby');
    });
});
时间: 2024-08-03 10:59:17

AngularJS Test的相关文章

AngularJS中的自定义指令的使用介绍

  这篇文章主要介绍了深入讲解AngularJS中的自定义指令的使用,AngularJS是一款热门的JavaScript开发库,需要的朋友可以参考下 AngularJS的自定义指令,就是你自己的指令,加上编译器编译DOM时运行的原生核心函数.这可能很难理解.现在,假设我们想在应用中不同页面复用一些特定的代码,而又不复制代码.那么,我们就可以简单地把这段代码放到单独的文件,并调用使用自定义指令的代码,而不是一遍又一遍地敲下来.这样的代码更容易理解.AngularJS中有四种类型的自定义指令: 元素

3个可以改善用户体验的AngularJS指令介绍

  这篇文章主要介绍了3个可以改善用户体验的AngularJS指令,AngularJS是一款具有很高人气的JavaScript框架,需要的朋友可以参考下 1.头像图片 为了在你的应用中展示头像图片,你需要使用用户的电子邮件地址,将地址转换为小写并使用md5加密该字符串.所以聪明的做法是使用指令来做到这些,并且可以复用. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29   /* * A

angularjs控制页面切换

问题描述 angularjs控制页面切换 <!DOCTYPE html> <html ng-app="ngView"> <head> <title>分页显示</title> <meta charset="utf-8"> <script src="js/jquery-1.9.1.js"></script> <script src="js/

使用AngularJS实现可伸缩的页面切换的方法

这篇文章主要介绍了使用AngularJS实现可伸缩的页面切换的方法,AngularJS是一款热门的JavaScript库,需要的朋友可以参考下 AngularJS 1.2 通过引入基于纯CSS class的切换和动画,在一个单页面应用创建页面到页面的切换变得更加的容易.只需要使用一个ng-view,让我们来看一下,一个引入众多的不同切换的可伸缩方法,以及指定的每个页面如何切入和切出. 演示: http://embed.plnkr.co/PqhvmW/preview 首先,标记: ? 1 2 3

angularjs和jersey的小问题

问题描述 angularjs和jersey的小问题 我想问一下项目用angularJS和jersey开发的话,jersey对应MVC三层体系中的哪个呢, 解决方案 jersey没有用过,刚才了解了一下 :Jersey是一个RESTFUL请求服务JAVA框架,与常规的JAVA编程使用的struts框架类似,它主要用于处理业务逻辑层.与Struts类似,它同样可以和hibernatespring框架整合.我经常用Struts就像把他当成struts吧.我经常用angularJS ,说一下Angula

php-PHP怎么获取获取angularjs http.post

问题描述 PHP怎么获取获取angularjs http.post 前端:$http({ method: 'POST' url: ""http://127.0.0.1:8080/school/studentadd.php"" data : newnames headers : {'Content-Type': 'application/x-www-form-urlencoded' } }).success(function(){ console.log('succe

AngularJS 全局scope与Isolate scope通信

在项目开发时,全局scope 和 directive本地scope使用范围不够清晰,全局scope与directive本地scope通信掌握的不够透彻,这里对全局scope 和 directive本地scope的使用做一个总结. 一.scope作用域 1.AngularJS中,子作用域一般都会通过JavaScript原型继承机制继承其父作用域的属性和方法.但有一个例外:在 directive中使用scope: { ... },这种方式创建的作用域是一个独立的"Isolate"作用域,它

angularjs表达式-Expression

紧接上节谈到再谈angularjs DI(Dependency Injection),在这里介绍关于angularjs的表达式expression.expression指的 是javascript的一小片段代码,通常用于绑定(binding)例如:{{ expression }}.在angularjs中是通过$parse service解析 . $parse用法: $parse(expression); 参数:javascript代码片段. 返回值:{function(context, loca

再谈angularjs DI(Dependency Injection)

在前面已经介绍了关于angularjs,以及扩展了一些jQuery ui的一些组件为angularjs的directive.在这里应进口007 在上 篇留言我们来看看在angularjs中的DI特性. DI:依赖注入,是一种软件设计模式,应DIP依赖倒置原则,描述组件之间 高层组件不应该依赖于底层组件.依赖倒置是指实现和接口倒置,采用自顶向下的方式关注所需的底层组件接口,而不是其实现 .其应用框架则为IOC,在.net中有很多我们熟悉的IOC框架,如Unity,Castle windsor,Ni

Angularjs示例:Sonar中项目使用语言分布图

在博客中介绍google的Angularjs 客户端PM模式框架很久了,今天发布一个关于AngularJs使用是简单示例SonarLanguage(示例位于Github:https://github.com/greengerong/SonarLanguage).本项目只是一个全为客户端的示例项目.项目的初始是我想看看在公司的项目中使用语言的分布比例,看看C#的份额,这一年一直坐着Java项目,却还是喜欢着C#,这只是个人问题,不存在语言之争.公司的项目在持续集成CI后都会传递build数据到So