1
我對自動化測試絕對陌生,所以我從angular和Karma + Jasmine開始。Karma不運行任何測試(0 of 0錯誤)
這是我karma.conf.js
文件
// test/spec/components/lib/search.js
// Karma configuration
// http://karma-runner.github.io/0.12/config/configuration-file.html
// Generated on 2015-10-20 using
// generator-karma 1.0.0
module.exports = function(config) {
'use strict';
config.set({
// enable/disable watching file and executing tests whenever any file changes
autoWatch: true,
// base path, that will be used to resolve files and exclude
basePath: '../',
// testing framework to use (jasmine/mocha/qunit/...)
// as well as any additional frameworks (requirejs/chai/sinon/...)
frameworks: [
"jasmine"
],
// list of files/patterns to load in the browser
files: [
// bower:js
'bower_components/jquery/dist/jquery.js',
'bower_components/angular/angular.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/angular-ui-router/release/angular-ui-router.js',
'bower_components/angular-bootstrap/ui-bootstrap-tpls.js',
'bower_components/ngInfiniteScroll/build/ng-infinite-scroll.js',
'bower_components/Chart.js/Chart.js',
'bower_components/angular-chart.js/dist/angular-chart.js',
'bower_components/bootstrap-sass/assets/javascripts/bootstrap.js',
'bower_components/angular-mocks/angular-mocks.js',
// endbower
// "app/components/lib/search.js",
// "test/mock/**/*.js",
"test/spec/**/*.js"
],
// list of files/patterns to exclude
exclude: [
],
// web server port
port: 8080,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: [
"PhantomJS"
],
// Which plugins to enable
plugins: [
"karma-phantomjs-launcher",
"karma-jasmine"
],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false,
colors: true,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// Uncomment the following lines if you are using grunt's server to run the tests
// proxies: {
// '/': 'http://localhost:9000/'
// },
// URL root prevent conflicts with the site root
// urlRoot: '_karma_'
});
};
,這是我的單元測試
console.log(1);
describe('test sul modulo search', function() {
console.log(2);
beforeEach(module('search'));
describe('searchObject', function() {
console.log(3);
var SearchObject;
beforeEach(inject(function(_SearchObject_){
console.log(4);
// The injector unwraps the underscores (_) from around the parameter names when matching
SearchObject = _SearchObject_;
//it(...)
}));
});
});
基本上我檢查給定對象的服務結構。 我跑grunt test
,一切似乎很正常,直到這個
Running "karma:unit" (karma) task
10 02 2016 17:14:25.645:INFO [karma]: Karma v0.13.19 server started at http://lo
calhost:8080/
10 02 2016 17:14:25.672:INFO [launcher]: Starting browser PhantomJS
10 02 2016 17:14:28.578:INFO [PhantomJS 2.1.1 (Windows 8 0.0.0)]: Connected on s
ocket /#x_Xg4CHTyVCYljSGAAAA with id 76913427
PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 1
PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 2
PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 3
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 0 of 0 ERROR (0.006 secs/0 secs)
Warning: Task "karma:unit" failed. Use --force to continue.
聽起來就像是沒有看到我的測試
編輯。我重新寫了我的測試作爲跟隨(console.logs添加):我只看到前3
編輯2:it
塊丟失,它一切工作正常。 Howerev現在測試失敗,因爲對象未定義。這裏是新的測試代碼:
describe('test sul modulo search', function() {
console.log(2);
beforeEach(module('search'));
describe('SearchObject test', function() {
console.log(3);
var SearchObject;
beforeEach(inject(function(_SearchObject_){
console.log(4);
// The injector unwraps the underscores (_) from around the parameter names when matching
SearchObject = _SearchObject_;
}));
describe('is defined', function() {
it('should evaluate the injected SearchObject', function(){
console.log(5);
expect(SearchObject).toBeDefined()
});
});
});
});
這裏是jasmine
Running "karma:unit" (karma) task
11 02 2016 09:22:29.063:INFO [karma]: Karma v0.13.19 server started at http://lo
calhost:8080/
11 02 2016 09:22:29.094:INFO [launcher]: Starting browser PhantomJS
11 02 2016 09:22:31.833:INFO [PhantomJS 2.1.1 (Windows 8 0.0.0)]: Connected on s
ocket /#XlTyAkmOHjoihkAyAAAA with id 2744252
PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 1
PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 2
PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 3
LOG: 5
PhantomJS 2.1.1 (Windows 8 0.0.0) test sul modulo search SearchObject test is de
fined should evaluate the injected SearchObject FAILED
C:/Users/Rick/Sviluppo/socialsider-fe/bower_components/angular/angular.j
s:4459:53
[email protected]:/Users/Rick/Sviluppo/socialsider-fe/bower_components/angular/a
ngular.js:340:24
[email protected]:/Users/Rick/Sviluppo/socialsider-fe/bower_components/angul
ar/angular.js:4419:12
[email protected]:/Users/Rick/Sviluppo/socialsider-fe/bower_components/an
gular/angular.js:4344:22
[email protected]:/Users/Rick/Sviluppo/socialsider-fe/bower_components/angular-mo
cks/angular-mocks.js:2428:60
Expected undefined to be defined.
C:/Users/Rick/Sviluppo/socialsider-fe/test/spec/search.js:19:49
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 1 of 1 (1 FAILED) (0 secs/0.04 sec
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.032 secs
/0.04 secs)
輸出正如你可以看到log(4)
丟失,這是關係到SearchObject
分配塊。這是問題嗎?
謝謝你,現在thest運行。但這裏還有另一個問題:它對我說那個未定義的對象。爲什麼? – Nemus
你可以在這裏看到它:http://stackoverflow.com/questions/35337492/jasmine-doesnt-inject-services – Nemus