Node.js教程 结合angularjs,Karma和Jasmine自动化单元测试
沉沙 2018-10-19 来源 : 阅读 1166 评论 0

摘要:本篇教程介绍了Node.js教程 结合angularjs,Karma和Jasmine自动化单元测试,希望阅读本篇文章以后大家有所收获,帮助大家对Node.js的理解更加深入。

本篇教程介绍了Node.js教程 结合angularjs,Karma和Jasmine自动化单元测试,希望阅读本篇文章以后大家有所收获,帮助大家对Node.js的理解更加深入。

<

1. Karma的介绍
Karma是Testacular的新名字,在2012年google开源了Testacular,2013年Testacular改名为Karma。Karma是一个让人感到非常神秘的名字,表示佛教中的缘分,因果报应,比Cassandra这种名字更让人猜不透!
Karma是一个基于Node.js的JavaScript测试执行过程管理工具(Test Runner)。该工具可用于测试所有主流Web浏览器,也可集成到CI(Continuous integration)工具,也可和其他代码编辑器一起使用。这个测试工具的一个强大特性就是,它可以监控(Watch)文件的变化,然后自行执行,通过console.log显示测试结果。
Jasmine是单元测试框架,本单将介绍用Karma让Jasmine测试自动化完成。Jasmine的介绍,请参考文章:jasmine行为驱动,测试先行
istanbul是一个单元测试代码覆盖率检查工具,可以很直观地告诉我们,单元测试对代码的控制程度。
2. Karma的安装
系统环境:
win7 64bit, node v0.10.5, npm 1.2.19首先,安装Git工具,然后用以下命令从Github复制以angular-seed为基础的phonecat项目:git clone git://github.com/angular/angular-phonecat.git解压到D:\web\angular-phonecatapp目录是存放源码,主文件的。test目录是存放单元测试文件的。安装项目依赖的angular等包文件D:\web\angular-phonecat>bower install安装KarmaD:\web\angular-phonecat>npm install -g karma-cli测试是否安装成功D:\web\angular-phonecat>karma --versionKarma version: 0.12.24
3. Karma + Jasmine配置
初始化karma配置文件karma.conf.js

D:\web\angular-phonecat>cd test
D:\web\angular-phonecat\test>karma init
Which testing framework do you want to use ? Press tab to list possible options. Enter to move to the next question. 
> jasmine
Do you want to use Require.js ? This will add Require.js plugin. Press tab to list possible options. Enter to move to the next question.
> yes
Do you want to capture any browsers automatically ? Press tab to list possible options. Enter empty string to move to the next quest ion. 
> Chrome 
>
What is the location of your source and test files ? You can use glob patterns, eg. "js/*.js" or "test/**/*Spec.js". Enter empty string to move to the next question. 
>
Should any of the files included by the previous patterns be excluded ? You can use glob patterns, eg. "**/*.swp". Enter empty string to move to the next question. 
>
Do you wanna generate a bootstrap file for RequireJS? This will generate test-main.js/coffee that configures RequireJS and starts the tests. 
> no
Which files do you want to include with <script> tag ? This should be a script that bootstraps your test by configuring Require.js and kicking __karma__.start(), probably your test-main.js file. Enter empty string to move to the next question. 
>
Do you want Karma to watch all the files and run the tests on change ? Press tab to list possible options. 
> yes
Config file generated at "D:\web\angular-phonecat\test\karma.conf.js".

 
4. 自动化单元测试
3步准备工作:

1. 创建源文件:用于实现某种业务逻辑的文件,就是我们平时写的js脚本
2. 创建测试文件:符合jasmineAPI的测试js脚本
3. 修改karma.conf.js配置文件

1). 创建源文件:用于实现某种业务逻辑的文件,就是我们平时写的js脚本
controllers.js,在html中ng-repeat循环输出phones

var phonecatApp = angular.module(‘phonecatApp‘, []);

phonecatApp.controller(‘PhoneListCtrl‘, function($scope) {
  $scope.phones = [
    {‘name‘: ‘Nexus S‘,
     ‘snippet‘: ‘Fast just got faster with Nexus S.‘},
    {‘name‘: ‘Motorola XOOM™ with Wi-Fi‘,
     ‘snippet‘: ‘The Next, Next Generation tablet.‘},
    {‘name‘: ‘MOTOROLA XOOM™‘,
     ‘snippet‘: ‘The Next, Next Generation tablet.‘}
  ];
});

2). 创建测试文件:符合jasmineAPI的测试js脚本
controllersSpec.js

describe(‘PhoneCat controllers‘, function() {

  describe(‘PhoneListCtrl‘, function(){

    beforeEach(module(‘phonecatApp‘));

    it(‘should create "phones" model with 3 phones‘, inject(function($controller) {
      var scope = {},
          ctrl = $controller(‘PhoneListCtrl‘, {$scope:scope});

      expect(scope.phones.length).toBe(3);
    }));

  });
});

3). 修改karma.conf.js配置文件我们这里需要修改:files和exclude变量

module.exports = function(config){
  config.set({
    basePath : ‘../‘,
    files : [
      ‘app/bower_components/angular/angular.js‘,
      ‘app/bower_components/angular-route/angular-route.js‘,
      ‘app/bower_components/angular-mocks/angular-mocks.js‘,
      ‘app/js/**/*.js‘,
      ‘test/unit/**/*.js‘
    ],
    autoWatch : true,
    frameworks: [‘jasmine‘],
    browsers : [‘Chrome‘],
    plugins : [
            ‘karma-chrome-launcher‘,
            ‘karma-jasmine‘
            ],
    junitReporter : {
      outputFile: ‘test_out/unit.xml‘,
      suite: ‘unit‘
    }
  });
};

启动karma

D:\web\angular-phonecat\test>karma start
INFO [karma]: Karma v0.12.24 server started at //localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 37.0.2031 (Windows 7)]: Connected on socket XwUVHF6Pm0tqa67igE3O wi
th id 65679142
Chrome 37.0.2031 (Windows 7): Executed 1 of 1 SUCCESS (0.013 secs / 0.011 secs)

浏览器自动打开

修改controllersSpec.js,保存后,会自动执行单元测试。
     

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注WEB前端Node.js频道!

本文由 @沉沙 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程