使用Yeoman搭建 AngularJS 应用 (10) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/write-unit-tests.html
对于不熟悉的Karma的人来说,这是JavaScript测试框架,这个Angular的生成器包含了两个测试框架: ngSenario和Jasmine,当我们之前运行yo angular创建了一个karma.conf.js文件。并且获取Karma的Node模块。我们将编辑一个Jasmine来测试我们的项目。
运行单元测试
让我们回到命令行,使用Ctrl + C来关闭Grunt服务。这已经存在于Gruntfile.js基架中。输入下面的命令
grunt test
当我们运行 grunt test,你将看到Yeoman界面中的警告,不要担心,我们来修复它。
记得查看grunt当前的端口有没有被占用,否则会出现错误的。
更新Karma的配置
第一,我们需要检查Karma配置来看我们是否安装了最新的Bower
打开karma.conf.js。现在这个files看起来是这样的
files: [
// bower:js
'bower_components/jquery/dist/jquery.js',
'bower_components/angular/angular.js',
'bower_components/bootstrap/dist/js/bootstrap.js',
'bower_components/angular-animate/angular-animate.js',
'bower_components/angular-cookies/angular-cookies.js',
'bower_components/angular-resource/angular-resource.js',
'bower_components/angular-route/angular-route.js',
'bower_components/angular-sanitize/angular-sanitize.js',
'bower_components/angular-touch/angular-touch.js',
'bower_components/jquery-ui/jquery-ui.js',
'bower_components/angular-ui-sortable/sortable.js',
'bower_components/angular-mocks/angular-mocks.js',
// endbower
'app/scripts/**/*.js',
'test/mock/**/*.js',
'test/spec/**/*.js'
],
Bower已经读取了新的依赖文件,并且自动的添加了karma.conf.js文件
更新单元测试
你在test/spec/controllers/main.js更改代码如下
it('should attach a list of awesomeThings to the scope', function () {
expect(scope.awesomeThings.length).toBe(3);
});
然后代替成如下代码
it('should have no items to start', function () {
expect(scope.todos.length).toBe(0);
});
打开scripts/controllers/main.js
删除$scope.todos中的3个元素
$scope.todos = [];
重新运行grunt test,你将看到测试通过
如果出现下面的错误
那就输入下面的命令
npm install grunt-karma karma karma-phantomjs-launcher karma-jasmine jasmine-core phantomjs --save-dev
添加更多的单元测试
让我们添加更多的单元测试
it('should add items to the list', function () {
scope.todo = 'Test 1';
scope.addTodo();
expect(scope.todos.length).toBe(1);
}); it('should add then remove an item from the list', function () {
scope.todo = 'Test 1';
scope.addTodo();
scope.removeTodo(0);
expect(scope.todos.length).toBe(0);
});
MainCtrl测试如下所示(test/spec/controllers/main.js)
'use strict'; describe('Controller: MainCtrl', function () { // load the controller's module
beforeEach(module('mytodoApp')); var MainCtrl,
scope; // Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
MainCtrl = $controller('MainCtrl', {
$scope: scope
});
})); it('should have no items to start', function () {
expect(scope.todos.length).toBe(0);
}); it('should add items to the list', function () {
scope.todo = 'Test 1';
scope.addTodo();
expect(scope.todos.length).toBe(1);
}); it('should add then remove an item from the list', function () {
scope.todo = 'Test 1';
scope.addTodo();
scope.removeTodo(0);
expect(scope.todos.length).toBe(0);
}); });
再次运行测试,我们会得到下面结果
测试成功
使用Yeoman搭建 AngularJS 应用 (10) —— 让我们搭建一个网页应用的更多相关文章
- 使用Yeoman搭建 AngularJS 应用 (12) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/local-storage.html 安装Bower程序包 我们使用另一个Angular模块,"angular-local-sto ...
- 使用Yeoman搭建 AngularJS 应用 (5) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/scaffold-app.html 基架 (Scaffolding) 在Yeoman中的意思是为基于你特殊的配置需求,为网站程序生成文件的工 ...
- 使用Yeoman搭建 AngularJS 应用 (4) —— 让我们搭建一个网页应用
在开发一个的网页传统工作流程中,你需要大量的时间去设置引用文件,下载依赖文件,并且手动的创建网页文件结构.Yeoman生成器将会帮助你完成这些.让我们安装一个AngularJS项目的生成器. 安装An ...
- 使用Yeoman搭建 AngularJS 应用 (3) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/setup.html 与Yeoman的交互大多数是通过命令行.在苹果机器需要使用Terminal应用,在Linux使用shell.如果使用W ...
- 使用Yeoman搭建 AngularJS 应用 (2) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/index.html 使用Yeoman搭建简单的应用 今天将会搭建一个简单的网页程序.你将可以添加,删除,拖拽和保存. 浏览Yeoman Y ...
- 使用Yeoman搭建 AngularJS 应用 (11) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/prepare-production.html 让我们发布这个应用 优化产品的文件 为了创建应用的产品版本,我们想做如下的事情 检查你的代码 ...
- 使用Yeoman搭建 AngularJS 应用 (9) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/install-packages.html 列出当前程序包 我们现在查看一下我们已经安装的程序包,输入下面的命令 bower list 查找 ...
- 使用Yeoman搭建 AngularJS 应用 (8) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/write-app.html 创建一个新的模板来显示一个todo的列表 打开views/main.html 为了从一个干净的模板开始,删除m ...
- 使用Yeoman搭建 AngularJS 应用 (7) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/preview-inbrowser.html 开启你的服务 运行Grunt任务,通过输入下面的命令来创建一个本地Node的http服务,地址 ...
随机推荐
- c# 与 PHP中 SHA1加密结果不同解决方法
那天在调试API的时候,发现用c#写的SHA1加密出来的结果和PHP中sha1()出来的不一样,找了半天的原因后来才弄出来 在调试微信接口的时候大多的帮助文档都是提供的是PHP的方法,所以在.net中 ...
- 【AngularJs】---$sce 输出Html
[问题描述] angular js的强大之处之一就是他的数据双向绑定功能----->ng-bind和针对form的ng-model 但在我们的项目当中会遇到这样的情况,后台返回的数据中带有各种各 ...
- git常用命令行
查看.添加.提交.删除.找回,重置修改文件 git help <command> # 显示command的help git show # 显示某次提交的内容 git show $id gi ...
- Cocos开发中Visual Studio下libcurl库开发环境设置
我们介绍一下win32中Visual Studio下libcurl库开发环境设置.Cocos2d-x引擎其实已经带有为Win32下访问libcurl库,Cocos2d-x 3.x中libcurl库文件 ...
- SpringMVC接收多个对象
问题背景: 要在一个表单里同时一次性提交多名乘客的个人信息到SpringMVC,前端HTML和SpringMVC Controller里该如何处理? 第1种方法:将Json对象序列化成Json字符串提 ...
- Bootstrap学习笔记(二) 表单
在Bootstrap学习笔记(一) 排版的基础上继续学习Bootstrap的表单,编辑器及head内代码不变. 3-1 基础表单 单中常见的元素主要包括:文本输入框.下拉选择框.单选按钮.复选按钮.文 ...
- Alluxio1.0.1最新版(Tachyon为其前身)介绍,+HDFS分布式环境搭建
Alluxio(之前名为Tachyon)是世界上第一个以内存为中心的虚拟的分布式存储系统.它统一了数据访问的方式,为上层计算框架和底层存储系统构建了桥梁. 应用只需要连接Alluxio即可访问存储在底 ...
- lex&yacc
LEX: yytext 数组包含匹配模式的文本; 使词法分析程序工作的两条规则是:1. lex 模式只匹配输入字符或字符串一次.2. lex 执行当前输入的最长可能匹配的动作. 由 lex 产生的词法 ...
- SAP校园招聘笔试
一直就向往着SAP公司,终于,有幸今天参加了SAP校园招聘的笔试.下面我就来简单说说这个笔试的内容. 笔试分为两大部分,一部分是逻辑题,就是些什么阅读分析计算balabala的一堆,是全英文的.另外一 ...
- 韩顺平细说Servlet视频系列意外收获之用命令行编译带有包的java类解决方案
命令行编译带有包的java类 在命令行编译这一块,基本上都是新手入门时了解一下,然后就直奔IDE而去.这样固然没错,就怕那些--.然后今天在视频中看到了这种方法,觉得可能会用到,所以就记录下来了,以备 ...