이 작업을 성공적으로 수행하기 위해 인터넷에서 찾은 모든 것을 시도했습니다. 내 서비스에서 기능을 테스트하려고했지만 내 서비스 범위에 따라 액세스하지 않습니다. 어떤 도움을 크게 감상 할 수있다 :)서비스 테스트 방법 - AngularJS/Jasmine
서비스 :
'use strict';
angular.module('Service').service('configService', function(
$rootScope, $http) {
var configObj = null;
return {
getConfig: function() {
if (configObj != null) {
console.log("returning cached config");
return configObj;
}
else {
return $http.get('conf.json').then(function(res) {
$http.get(res.confLocation).then(function(
locationResponse) {
configObj = locationResponse;
$rootScope.configObj = configObj;
console.log($rootScope.configObj);
return configObj;
});
});
}
}
};
});
getConfig 내가 시도한 시험에서 액세스되고 있지 않습니다를.
ServiceTests는 :
'use strict';
describe('Service: configService', function() {
// load the controller's module
beforeEach(module('Service'));
var configService, $httpBackend, results, tstLocation, tstRes;
var tstConfig = {
"confLocation": "local-dev-conf.json"
};
var tstConfigEmpty = {};
var tstConfigObjEmpty = {};
var tstConfigObj = {
"AWS": {
"region": "us-east-1",
"endpoint": "http://localhost:8133"
}
};
// Initialize the controller and a mock scope
beforeEach(inject(function(_configService_, _$httpBackend_) {
inject(function($rootScope) {
$rootScope.USERNAME = 'TESTER';
$rootScope.configObj = tstConfigObj;
$rootScope.locationResponse = tstLocation;
$rootScope.res = tstRes;
});
configService = _configService_;
$httpBackend = _$httpBackend_;
//Problem here??
spyOn(configService, 'getConfig').and.callFake(function() {
return {
then: function() {
return "something";
}
};
});
}));
it('should return a promise', function() {
expect(configService.getConfig().then).toBeDefined();
});
it('should test backend stuff', inject(function() {
results = configService.getConfig(tstConfig);
$httpBackend.expectGET('conf.json').respond(tstConfig);
$httpBackend.expectGET('local-dev-conf.json').respond(tstConfigObj);
$httpBackend.flush();
}));
//Thanks Miles
it('should check if it was called', inject(function() {
results = configService.getConfig().then();
expect(configService.getConfig).toHaveBeenCalled();
});
// console.log(results);
}));
it('should check for a null configObj', inject(function() {
results = configService.getConfig(tstConfigObjEmpty).then(function() {
expect(results).toBe(null);
});
// console.log(results);
// console.log(tstConfigObj);
}));
it('should check for a non-null configObj', inject(function() {
results = configService.getConfig(tstConfigObj).then(function() {
// Any string is accepted right now -- Why??
expect(results).toEqual("returning cached config");
expect(results).toBe("returning cached config");
expect(results).toBe("your mom"); // SHOULDN'T BE WORKING BUT DOES
expect(results).toEqual("Object{AWS: Object{region: 'us-east-1', endpoint: 'http://localhost:8133'}}");
expect(results).toBe("Object{AWS: Object{region: 'us-east-1', endpoint: 'http://localhost:8133'}}");
});
// console.log(results);
// console.log(tstConfigObj);
}));
it('should check for null file', inject(function() {
results = configService.getConfig(tstConfigEmpty).then(function() {
expect(results).toEqual(null);
expect(results).toBe(null);
});
}));
it('should test a valid file', inject(function() {
results = configService.getConfig(tstConfig).then(function() {
expect(results).not.toBe(null);
expect(results).toEqual("Object{confLocation: 'local-dev-conf.json'}");
})
});
나는 내가 잘못 spyOn를 사용하거나 제대로 내 테스트에서 getConfig에 접근하고 있지 않다 생각합니다. 생각?
편집 2 : 변경 테스트 마일하지만 테스트 범위에 여전히 업데이트로 발견되는 문제에 대한 3 개 감사합니다. Amy가 지적한 것처럼 내 스파이 로직에 문제가 있습니다. 나는 callFake를 사용해서는 안된다.
EDIT 3 : 지금 마일 덕분에 기능에 액세스했습니다.
spyOn(configService, 'getConfig').and.callThrough();
후 추가 테스트 케이스 :는 내 spyOn을 변경했다
는results = configService.getConfig(tstConfig).then();
expect(configService.getConfig).toHaveBeenCalled();
Coverage now (still needs work)
and.callFake 대신 and.allerValue를 시도한 후 해당 값을 반환 했습니까? –
I는 and.returnValue and.callFake으로 교체 시도뿐만 아니라 : "spyOn (configService 'getConfig') and.returnValue (함수() { 창 { 다음 :. 함수() { 복귀" 내가 할 때 내 9 개의 테스트 중 6 개가 충돌 함 – FF5Ninja
다음 라인에서 함수가 호출되었는지 확인할 필요가 없습니다 _ _ 호출했습니다. –