我試圖嘲笑我正在使用的服務,應該返回一個承諾,模擬服務正在被調用,但我無法得到我的測試結果。被測試無法獲得承諾角度服務的結果
服務功能:
function getDevices() {
console.log('getDevices');
return servicesUtils.doGetByDefaultTimeInterval('devices')
.then(getDevicesComplete);
function getDevicesComplete(data) {
console.log('getDevicesComplete');
var devices = data.data.result;
return devices;
}
}
我的測試是:
describe('devicesService tests', function() {
var devicesService;
var servicesUtils, $q, $rootScope;
beforeEach(function() {
servicesUtils = {};
module('app.core', function ($provide) {
servicesUtils = specHelper.mockServiceUtils($provide, $q, $rootScope);
});
inject(function (_devicesService_, _$q_, _$rootScope_) {
devicesService = _devicesService_;
$q = _$q_;
$rootScope = _$rootScope_.$new();
});
});
it('getting device list', function() {
console.log('getting device list');
devicesService.getDevices().then(function (result) {
console.log(result);
expect(result).toBeDefined();
});
});
});
模擬文件:
function mockServiceUtils($provide, $q) {
var servicesUtils = {};
servicesUtils.doGetByDefaultTimeInterval = jasmine.createSpy().and.callFake(function() {
var deferred = $q.defer();
deferred.resolve('Remote call result');
$rootScope.$digest();
return deferred.promise;
});
$provide.value('servicesUtils', servicesUtils);
return servicesUtils;
}
你需要使用$ rootScope $爲了消化的承諾火 –
@DayanMorenoLeon我增加了消化,但雖然範圍定義摘要方法是不確定的。 ,我修改了我的問題 –
以及你的模擬沒有定義範圍,你的測試ddi。所以嘗試在getDevices被調用後執行摘要 –