javascript karma 测试运行程序没有运行任何测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25199900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
karma test runner not running any tests
提问by Apprentice Programmer
I'm using karma with jasmine and followed online guide by installing using
我将 karma 与 jasmine 一起使用,并按照在线指南安装使用
npm install --save-dev karma
and other necessities
和其他必需品
i ran
我跑了
./node_modules/karma/bin/karma start
and
和
karma start karma.conf.js
which opened up a external chrome browser showing that karma is connected. I wrote a simple unit test for one my functions it seems its not running any tests at all
它打开了一个外部 chrome 浏览器,显示 karma 已连接。我为我的一个函数编写了一个简单的单元测试,它似乎根本没有运行任何测试
This is my karma config file.
这是我的业力配置文件。
// Karma configuration
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'app/assets/components/angular/angular.js',
'app/assets/components/angular-mocks/angular-mocks.js',
'app/assets/javascripts/**/**/*.js',
'spec/javascripts/**/*.js'
],
// list of files / patterns to exclude
exclude: [],
// web server port
port: 8080,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['Chrome'],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
});
};
my unit test
我的单元测试
describe('Unit: AddMedicalService',function(){
beforeEach(module('DoctiblePreTreatment'));
var ctrl, scope;
beforeEach(inject(function($controller,$rootScope){
scope = $rootScope.$new();
ctrl = $controller('AddMedicalServiceModalCtrl',{
$scope: scope
});
}));
it('should create return true if var 1 is greater than var2 , false if other wise',
function(){
var compare1 = function(){
var var1 = 1;
var var2 = 0;
return var1 > var2;
}
var compare2 = function(){
var var1 = 0;
var var2 = 1;
return var1 > var2;
}
expect(compare1).toBeTruthy();
expect(compare2).toBeFalsy();
});
});
the particular function in the controller im trying to test
我正在尝试测试的控制器中的特定功能
(function() {
app.controller('AddMedicalServiceModalCtrl',['ProviderMedicalService','Treatment','$scope','$modalInstance',function(ProviderMedicalService,Treatment,$scope,$modalInstance){
$scope.newTreatment = {}
$scope.checkless = function(var1,var2){
var1 = parseInt(var1);
var2 = parseInt(var2);
if(var1 > var2){
return true;
}
else{
return false;
}
}
}]);
})();
what is displayed on the console when i run karma
当我运行 karma 时,控制台上会显示什么
INFO [karma]: Karma v0.12.21 server started at http://localhost:8080/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 36.0.1985 (Mac OS X 10.9.4)]: Connected on socket MkqZfXcO6iIX4Od23QEr with id 9498055
Additional info: I'm using angular-js with ruby on rails. I'm aware that there is the jasmine gem out there that can help me. But my boss insisted that we should try using karma to do our unit testing/E2E for anuglarjs portion and rspec for rails.
附加信息:我在 Rails 上使用带有 ruby 的 angular-js。我知道有茉莉花宝石可以帮助我。但是我的老板坚持认为我们应该尝试使用 karma 为 anuglarjs 部分和 rspec 进行单元测试/E2E 为 rails 做我们的单元测试/E2E。
回答by Mahesh Sapkal
Under karma.config.js
, set either singleRun
or autoWatch
to true
. In your case both of them are set to false, hence karma is not running the tests.
在 下karma.config.js
,将singleRun
或设置autoWatch
为true
。在您的情况下,它们都设置为 false,因此 karma 没有运行测试。
singleRun:If true, it captures browsers, runs tests and exits with 0 exit code (if all tests passed) or 1 exit code (if any test failed).
singleRun:如果为 true,它会捕获浏览器、运行测试并以 0 退出代码(如果所有测试通过)或 1 退出代码(如果任何测试失败)退出。
singleRun: true
autoWatch:Enable or disable watching files and executing the tests whenever one of these files changes. Incase you want to watch your files.
autoWatch:启用或禁用监视文件并在这些文件之一发生更改时执行测试。以防万一你想看你的文件。
autoWatch: true
回答by Sidharth Taneja
Following configuration works for me -
以下配置对我有用 -
reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO, // config.LOG_DEBUG,
autoWatch: true,
browsers: ['ChromeNS'],
singleRun: false,
customLaunchers: {
ChromeHeadlessNS: {
base: 'ChromeHeadless',
flags: ['--no-sandbox', '--disable-gpu']
},
ChromeNS: {
base: 'Chrome',
flags: ['--no-sandbox', '--disable-gpu']
}
}