我是使用茉莉和业力进行单元测试的新手,我想测试我的服务消耗api休息但是当我尝试模拟我的模块时,我总是在我的终端运行我的测试时得到一些消息instanctation失败使用业力这是我的文件:
我的模块:
'use strict';
// create the module and name it mainAPp
var app = angular.module('mainApp', ['ngRoute','LocalStorageModule','jkAngularRatingStars']);
// configure routes
app.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/home.html',
controller : 'mainController'
})
.when('/login', {
templateUrl: 'views/login.html',
controller : 'loginController'
})
.otherwise({
redirectTo: '/'
});
})
;
这是我想用茉莉花测试的服务
'use strict';
app.factory('authService', ['$http', '$q', '$log', 'localStorageService', function ($http, $q, $log, localStorageService) {
// base authentification url
var serviceBase = 'http://localhost:3000/user/auth';
var serviceLogOutUrl = 'http://localhost:3000/user/logout';
var authServiceFactory = {};
var _authentication = {
isAuth: false,
userName: "",
status: ""
}
//User authentification
var _login = function (loginData) {
var deferred = $q.defer();
$http.post(serviceBase, loginData, { headers: { 'Content-Type': 'application/json' } }).then(function (response) {
if (response.data.status == "success") {
localStorageService.set('sessionId', { sessionId: response.data.sessionId, userName: loginData.username });
_authentication.userName = loginData.username;
_authentication.isAuth = true;
}
else {
_logOut();
}
deferred.resolve(response);
}).catch(function (err, status) {
_logOut();
//$log.log('authentification failed.');
deferred.reject(err);
});
return deferred.promise;
};
// fill data
var _fillAuthData = function () {
var authData = localStorageService.get('sessionId');
if (authData) {
_authentication.isAuth = true;
_authentication.userName = authData.userName;
}
}
// check if the user is loged
var _islogin = function () {
_fillAuthData();
return _authentication.isAuth;
};
// logout function
var _logOut = function () {
var deferred = $q.defer();
if (localStorageService.get('sessionId') != null) {
var sessionId = localStorageService.get('sessionId').sessionId;
$http.get(serviceLogOutUrl + "?sessionId=" + sessionId, { headers: { 'Content-Type': 'application/json' } }).then(function (response) {
localStorageService.remove('sessionId');
_authentication.isAuth = false;
_authentication.userName = "";
if (response.status = "success") {
//$log.log('logout success.');
}
else {
//$log.log('logout failed.');
}
deferred.resolve(response);
}).catch(function (err, status) {
localStorageService.remove('sessionId');
_authentication.isAuth = false;
_authentication.userName = "";
deferred.reject(err);
});
return deferred.promise;
}
};
authServiceFactory.login = _login;
authServiceFactory.islogin = _islogin;
authServiceFactory.fillAuthData = _fillAuthData;
authServiceFactory.logOut = _logOut;
return authServiceFactory;
}]);
这是我的茉莉 specRunner.html :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Jasmine Spec Runner v2.6.3</title>
<link rel="shortcut icon" type="image/png" href="lib/jasmine-2.6.3/jasmine_favicon.png">
<link rel="stylesheet" href="lib/jasmine-2.6.3/jasmine.css">
<script src="lib/jasmine-2.6.3/jasmine.js"></script>
<script src="lib/jasmine-2.6.3/jasmine-html.js"></script>
<script src="lib/jasmine-2.6.3/boot.js"></script>
<script src="lib/angular/angular.js"></script>
<script src="lib/angular/angular-mocks.js"></script>
<script src="lib/angular/angular-route.js"></script>
<script src="lib/angular/rating-stars.js"></script>
<script src="lib/angular/angular-local-storage.min.js"></script>
<script src="lib/angular/app.js"></script>
<script src="lib/angular/authService.js"></script>
<script src="lib/angular/loginController.js"></script>
<!-- include source files here... -->
<script src="src/calculator.js"></script>
<!-- include spec files here... -->
<script src="spec/calculator.spec.js"></script>
</head>
<body>
</body>
</html>
这是我的业力配置部分 //要在浏览器中加载的文件/模式列表
档案:[ 'LIB /角度/ angular.js', 'LIB /角度/角mocks.js', 'LIB /角度/角route.js', 'LIB /角度/评级-stars.js', “LIB /角度/角和本地storage.min.js”, 'LIB /角度/ app.js', 'LIB /角度/ authService.js', 'LIB /角度/ loginController.js', 'SRC / / 的.js', '规格/ / 的.js' ],
这是我的单元测试,由于我的模块导致实例失败而无法运行:
describe("Services", function () {
var service, $httpBackend;
beforeEach(function(){
angular.mock.module('ngRoute', []);
angular.mock.module('LocalStorageModule', []);
angular.mock.module('jkAngularRatingStars', []);
module('mainApp', ['ngRoute','LocalStorageModule','jkAngularRatingStars']);
module(function ($provide) {
$provide.value('authService', service);
});
});
describe("auth services", function () {
beforeEach(inject(function($injector) {
service = $injector.get('authService');
$httpBackend = $injector.get('$httpBackend');
$httpBackend.when('POST', "http://localhost:3000/user/auth").respond('{ "status": "success", "sessionId": "a8t9Rr9bjWD2InfeFLbNS3FNg5mnFqiV", "username": "ali" }');
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('login - should return auth success', function () {
service.login().then(function(response) {
expect(response.status).toEqual('success');
});
$httpBackend.flush();
});
});
});
这是我的业力配置js:
// Karma configuration
// Generated on Tue Jun 27 2017 13:17:32 GMT+0000 (WET)
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: [
'lib/angular/angular.js',
'lib/angular/angular-mocks.js',
'lib/angular/angular-route.js',
'lib/angular/rating-stars.js',
'lib/angular/angular-local-storage.min.js',
'lib/angular/app.js',
'lib/angular/authService.js',
'lib/angular/loginController.js',
'src/**/*.js',
'spec/**/*.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,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
有人可以帮助我提前解决这个问题吗