AngularJS——karma的安装

1,前言:

  刚刚学过了 grunt的安装以及使用,grunt的作用就是让我们平常不想做的任务能够自动化完成,并且可以自己 自定义任务,那么karma是什么呢?

  Karma是Testcular的新名字,在2012年google开源了 Testcular,2013年改名 Karma。karma是一个让人感到非常神秘的名字,表示佛教中的缘分,因果报应。

  Karma的作用是 基于 Node.js的javascript测试执行过程管理工具。

2,Karma的安装

  因为我是win8的系统,所以你们懂得,而且我默认你们安装了Node.js

  1,安装karma-cli,

   打开我们 CMD面板,随便的一个位置输入 npm install -g karma-cli

   

  这样我们会把 karma  安装在我们系统的全局目录中去(C:UsersjennyNJAppDataRoaming
pm
ode_moduleskarma-cli),注意安装了 karma-cli并不是安装了karma,现在网上有教程说是直接使用命令 npm install -g karma 就可以了,但是我试过很多遍都不可以,或许是系统的原因吧,但是如果先安装 karma-cli,再安装 karma就可以了,

  2,安装 karma  

  可主动建立一个文件,用 CMD找到新建文件的目录,输入 npm install karma可从网络下载 karma

  

  3,运行 karma

  输入 karma start

  

  可以在 http://localhost:9876浏览,

  

   4,配置 karma 

    输入 karma init,会出现一系列的对话框,(提示下,我使用的是PowerCmd的一款命令工具,但是他的权限不够,所以我在 init 的时候,只能出现一个对话框,输入 enter 发现不能执行,只有在系统自带的命令框才可以,而且必须是已管理员身份运行的)

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.
> no

Do you want to capture a browser automatically ?
Press tab to list possible options. Enter empty string to move to the next question.
> 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 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:workspacejavascriptkarmakarma.conf.js".

  5,安装 jasmine

    运行 npm install karma-jasmine

    

   6,运行 karma

     运行karma最重要的是配置karma.conf.js,具体的配置请查询 karma官网文档,在此就做简单的介绍

//包装函数
module.exports = function(config) {
   //初始化配置
  config.set({
    ....
   })

}

     里面和我们上次介绍的 grunt的配置结构差不多,都有包装函数和初始化配置参数

    1,载入的框架有 jasmine 和 require.js

frameworks: ['jasmine'],

      2,要检测的文件,我现在设置的是所有文件

    files: [
      '*.js'
    ],

  3,要过滤的文件

xclude: [
        'karma.conf.js',
        'test-main.js'
    ],

  4,启动服务的端口

 port: 9876,

  5,启动的浏览器(会自动启动哦)

 browsers: ['Chrome'],

  6,设置响应超时

captureTimeout: 10000,

  

原文地址:https://www.cnblogs.com/zhiyuan-2011/p/4162581.html