首页 技术 正文
技术 2022年11月18日
0 收藏 353 点赞 4,753 浏览 3314 个字

首先讲一下大致的流程:

  • 需要node环境,首先先要安装node,node不会?请自行搜索.版本>0.8
  • 安装node完成之后先要测试下npm是否测试通过,如下图所示angular测试-Karma + Jasmine配置
  • 首先看下目录结构 目录为:F:\karma>

angular测试-Karma + Jasmine配置

  • 其中karma.config.js另外说,因为这个是安装karma之后,karma的运行完全依赖这个配置文件
  • 接下来安装karma
//为了能够让全局都可以运行karma的命令行
npm install -g karma-cli
//推荐全局,简单不出错
npm install karma -g --save-dev
//接下来安装你项目需要的组件, 这个是为了保证运行karma可有直接调用系统的chrome浏览器,如果没有chrome浏览器,还是建议安装吧,不然只能呵呵了..
npm install karma-jasmine karma-chrome-launcher -g --save-dev
  •  到现在为止基本完成80%了,接下来就是生成karma.config.js文件

angular测试-Karma + Jasmine配置

angular测试-Karma + Jasmine配置

  • enter完之后,在F:\karma\下就生成了karma.config.js
  • === 广告大法angular测试-Karma + Jasmine配置  === http://www.cnblogs.com/NetSos/p/4371075.html
  • 打开配置文件
// Karma configuration
// Generated on Mon Mar 23 2015 16:18:18 GMT+0800 (中国标准时间)module.exports = function(config) {
config.set({ // base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '', // frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'], // list of files / patterns to load in the browser
files: [
"js/plugin/angular.js",
"js/plugin//angular-mocks.js",
"js/*.js",
"tests/*.tests.js"
], // list of files to exclude
exclude: [
], // preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
}, // test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'], // web server port
port: 9876, // enable / disable colors in the output (reporters and logs)
colors: true, // level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO, // enable / disable watching file and executing tests whenever any file changes
autoWatch: true, // start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'], // Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
});
};
  • 每个配置的意思可以自行搜索,重要的配置在files这个配置里面,把必须的文件引入进去,其他的*表示就可以了
  • 由于咱们用的是jasmine测试框架,首先你需要了解jasmine的测试语法 duang~~ http://jasmine.github.io/edge/introduction.html
  • 接下来贴出home.js(angular写法的js) 和 对应测试文件home.tests.js,由于是测试js,目前是不需要html页面的
  • home.js
'use strict';
var app = angular.module('netsos.cnblogs.com',[]);
app.controller('Hevily',['$scope',function($scope){
$scope.text = 'hello';
}]);
  • home.tests.js
'use strict';
describe('Hevily',function(){
var scope;
//module都是angular.mock.module的缩写
beforeEach(module('netsos.cnblogs.com'));
//inject都是angular.mock.inject的缩写
beforeEach(inject(function($rootScope,$controller){
scope = $rootScope.$new();
$controller('Hevily',{$scope:scope});
}));
it('text = hello',function(){
expect(scope.text).toBe('hello');
});
});
  •   运行karma

angular测试-Karma + Jasmine配置

简单的单元测试入门就这样结束了,么么么….

点击下载示例代码: angular测试-Karma + Jasmine配置

如果是seajs的话 需要增加这个文件

(function(__karma__, seajs) {
var tests = [],
file;
// var alias = {
// 'testfile':'testfile/test',
// 'login':'webapp/resources/view/src/login',
// 'test':'testjs/test'
// } var alias = {};
for (file in __karma__.files) {
if (__karma__.files.hasOwnProperty(file)) {
// if (/test\.js$/i.test(file)) {
// tests.push(__karma__.basePath+file); //所有的测试用例代码文件以spec结尾
// }
// if (/ngjs/.test(file)) { var name = file.match(/([^.]+)\.js/)[]; //获取src目录下的文件路径作为seajs模块的key
alias[name] = name;
tests.push(name)
// console.log(tests)
// }
}
} seajs.config({
alias: alias
}) var __start = __karma__.start;
__karma__.start = function() {
seajs.use(['tests/epm.test'], function() {//测试文件的路径
__start.call(); //要在seajs模块载入后调用,否则会加载不到任何测试用例
// mocha.run()
});
};})(window.__karma__, seajs);
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898