我正在尝试在摩卡柴中测试工厂,但我收到了错误
'没有待处理的冲洗请求#39;。
我还申请了$rootScope.$apply();
无法确定究竟是什么问题
var Config, factory , $httpBackend ,authRequestHandler, servicedata,$rootScope;
beforeEach(function() {
module('modulename');
inject(function($injector) {
Config = $injector.get('Config');
factory = $injector.get('factoryname');
$httpBackend = $injector.get('$httpBackend');
$rootScope = $injector.get('$rootScope');
servicedata = {id:'1',name:'john'};
});
it('should test factory data', function () {
$httpBackend.whenGET('httpurl/sample/sample.data.json').respond(servicedata);
var promise = factory.functionname(),serviceResponseData;
promise.then(function (data) {
serviceResponseData = data;
});
$rootScope.$digest();
$httpBackend.flush();
expect(serviceResponseData.name).to.equal('john');
});
以下是工厂:
angular.module('module').factory('factoryname', function ($http, $q) {
var objectfactory;
objectfactory = {};
objectfactory.functionname = function() {
var url = 'httpurl/sample/' + 'sample.data.json'
var deferred = $q.defer();
$http.get(url)
.success(function(data) {
deferred.resolve(data);
});
return deferred.promise;
};
return objectfactory;
});