我有一个服务,我为我正在进行的项目编写,我正在尝试为它编写单元测试。以下是服务的代码
angular.module('services').factory('Authorization', ['$q', 'Refs', function($q, Refs) {
function isAuthorized() {
var deferred = $q.defer();
var authData = Refs.rootRef.getAuth();
var adminRef;
if(authData.google) {
adminRef = Refs.rootRef.child('admins').child(authData.uid);
} else {
adminRef = Refs.rootRef.child('admins').child(authData.auth.uid);
}
adminRef.on('value', function(adminSnap) {
deferred.resolve(adminSnap.val());
});
return deferred.promise;
}
return {
isAuthorized: isAuthorized
};
}]);
我已经为它编写了一个单元测试,但是无论何时我运行测试,我都会收到此错误消息'错误:超时 - 在jasmine.DEFAULT_TIMEOUT_INTERVAL指定的超时时间内未调用异步回调。'
以下是我为该服务编写的单元测试的代码:
'use strict';
describe('Authorization Service', function() {
var Refs, $q, Authorization, authData, scope, deferred;
beforeEach(angular.mock.module('Sidetime'));
beforeEach(inject(function(_Refs_, $rootScope, _$q_, _Authorization_) {
Refs = _Refs_;
$q = _$q_;
Authorization = _Authorization_;
}));
iit('return admin object', function(done) {
var result;
Authorization.isAuthorized = function() {
deferred = $q.defer();
authData = {google: 'uid:112222'};
if(authData.google) {
deferred.resolve(authData);
}
return deferred.promise;
};
Authorization.isAuthorized().then(function(result) {
console.log('result', result);
expect(result).toEqual(authData);
//done();
});
});
});
我不确定我是否正确编写单元测试。如果有人能够更好地为这项服务编写单元测试,我将不胜感激。感谢您的预期帮助。
答案 0 :(得分:1)
这是一个工作的plunker,略有修改,因为我没有所有依赖项的代码:
angular.module('services', []).factory('Authorization', ['$q', function($q) {
function isAuthorized() {
var deferred = $q.defer();
deferred.resolve({authData: {google: 'uid: 1122222'}});
return deferred.promise;
}
return {
isAuthorized: isAuthorized
};
}]);
describe('Authorization Service', function() {
var $q, Authorization, scope, deferred;
beforeEach(angular.mock.module('services'));
beforeEach(inject(function($rootScope, _$q_, _Authorization_) {
$q = _$q_;
scope = $rootScope.$new();
Authorization = _Authorization_;
Authorization.isAuthorized = function() {
deferred = $q.defer();
authData = {google: 'uid:112222'};
if(authData.google) {
deferred.resolve(authData);
}
return deferred.promise;
};
}));
it('return admin object', function(done) {
var result;
var promise = Authorization.isAuthorized();
promise.then(function(result) {
expect(result).toEqual(authData);
expect(result.google).toEqual('uid:112222e');
});
deferred.resolve(result);
scope.$digest();
});
});
(function() {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 250;
/**
Create the `HTMLReporter`, which Jasmine calls to provide results of each spec and each suite. The Reporter is responsible for presenting results to the user.
*/
var htmlReporter = new jasmine.HtmlReporter();
jasmineEnv.addReporter(htmlReporter);
/**
Delegate filtering of specs to the reporter. Allows for clicking on single suites or specs in the results to only run a subset of the suite.
*/
jasmineEnv.specFilter = function(spec) {
return htmlReporter.specFilter(spec);
};
/**
Run all of the tests when the page finishes loading - and make sure to run any previous `onload` handler
### Test Results
Scroll down to see the results of all of these specs.
*/
var currentWindowOnload = window.onload;
window.onload = function() {
if (currentWindowOnload) {
currentWindowOnload();
}
//document.querySelector('.version').innerHTML = jasmineEnv.versionString();
execJasmine();
};
function execJasmine() {
jasmineEnv.execute();
}
})();
答案 1 :(得分:0)
请查看spies in jasmine以获得更深入的介绍
我采用了Cognitroics plunkr并以我通常编写测试的方式对其进行了修改。在这里查看http://plnkr.co/edit/W5pP82CKj7tc6IO3Wj9S?p=preview
但基本上你应该做的就是利用茉莉花中间谍的精彩。
这是它的样子。
describe('My aweomse test suite', function(){
beforeEach(function(){
module('Awesome');
inject(function($injector){
this.MyService = $injector.get('MyService');
this.$q = $injector.get('$q');
this.$scope = $injector.get('$rootScope').$new();
spyOn(this.MyService, 'someMethod').andCallFake(function(){
var defer = this.$q.defer();
defer.resolve();
return defer.promise;
});
});
it('should do seomthing', function(){
// given
var result;
// when
this.MyServcie.someMethod().then(function(){
result = 'as promised';
});
this.$scope.$digest();
// then
expect(result).toEqual('as promised');
});
});
});