Karma kullanarak bir entegrasyon testi yazmaya çalışıyorum. Gerçek bir http araması yapmalı ve gerçek cevabı almalıyım. # 1434'te passthrough() kullanarak ve angular-mocks.js'de "$ httpBackend: angular.mock. $ HttpBackendProvider" yorumunu kullanan tüm çözümleri denedim ve bunların hiçbiri işe yaramadı. İşte Karma testte gerçek http araması yapabilir miyiz?
benim test kodu:use strict';
describe('module: main, service: AuthService', function() {
beforeEach(module('ngMockE2E'));
// load the service's module
beforeEach(module('main'));
// load all the templates to prevent unexpected $http requests from ui-router
beforeEach(module('ngHtml2Js'));
// instantiate service
var AuthService;
var $q;
var deferred;
var $rootScope;
var response;
var scope, http, flush, httpBackend;
beforeEach(inject(function ($httpBackend,AuthService,$q, $rootScope) {
httpBackend = $httpBackend;
AuthService = AuthService;
$q = $q;
$rootScope = $rootScope;
deferred = $q.defer();
scope = $rootScope.$new();
}));
it('should do something', function() {
expect(!!AuthService).toBe(true);
expect(AuthService).not.toBeNull();
});
it('verify authenticating user', function() {
AuthService.authUser('[email protected]','abc123').then(function (result) {
console.log("Result of successful auth user is ");
console.log(result);
expect(result).toEqual(true);
done();
}, function (error) {
console.log("error of failure auth user is ",error);
expect(false).toBe(true);
done();
});
httpBackend.whenPOST('/oauth/token').passThrough();
$rootScope.$digest();
//httpBackend.flush();
});
});
bu karma testi
Testler sırasında, bizim testler hızla çalıştırmak istiyoruz ve gerçek sunucuya XHR veya JSONP istekleri göndermek istemiyoruz hiçbir dış bağımlılıkları, sizin verilerine göre değiştirin. Tek ihtiyacımız olan, belirli bir isteğin gönderilip gönderilmediğini doğrulamak veya alternatif olarak uygulamanın yalnızca talepte bulunmasına izin vermek, önceden eğitilmiş cevaplarla yanıt vermek ve sonuçların beklediğimiz şey olduğunu iddia etmektir. – user1645290