Benhttp isteği için birim testi nasıl yazılır?
myService.getItem('/api/toy/' + scope.id).success(
function(toy) {
$scope.toy = toys.details;
}
);
MyService benim test denetleyicisi olarak benim durumumda bir birim testi
angular.module('toyApp').service('myService', ['$http',
function($http) {
var service = {};
return {
getItem: function(url) {
return $http.get(url);
},
};
}
]);
Testi dosyası
bir hizmeti yapmaya çalışıyorum.
describe('toy ctrl', function() {
var $httpBackend, ctrl, myService;
beforeEach(module('toyApp'));
beforeEach(inject(function (_$controller_, _$httpBackend_, _$rootScope_, __myService_) {
scope = _$rootScope_.$new();
$httpBackend = _$httpBackend_;
myService = _myService_;
ctrl = _$controller_('toyCtrl', {
$scope: scope
});
}));
describe('call my service', function() {
it('should make request when app loads', function() {
$httpBackend.expectGET('/api/toy/123').respond({id:123, detail:456 });
myService.getItem('/api/toy/123').then(function(toy){
expect(scope.toy.detail).toBe(456);
})
$httpBackend.flush();
})
})
ben $httpBackend.flush()
alırsan ben hata gitmiş,
Error: Unexpected request: GET /api/toy/123
No more request expected
edilir alıyorum ama
function(toy) {
$scope.toy = toys.details;
}
bölümünü karşılamayacaktır. Fonksiyon çağrısını kapatmak ve bunu nasıl yapacağımı bilmiyorum. Bana herkes yardım edebilir mi? Çok teşekkürler
misiniz ünite test kontrolörü veya servisi (uygulama yükleri açık olduğunda talepte bulunmalıdır) ke kontrolörü)? – PSL
benim denetleyicisi – FlyingCat
Tamam içeride hizmetini test ediyorum. Bu durumda servisinizi atabilmeniz ve kontrol cihazına enjekte edebilmeniz yeterlidir. Ve kapsam değerlerine karşı test değeri. Temelde ne ben söylemeye çalışıyorum sadece kontrolör mantığı 'myService.getItem ('/ API/oyuncak/123') test etmek zorunda olduğunda birim test denetleyicisi olan' aslında alakasız. – PSL