Angularjs 使用jasmine正确模拟angular中的服务
为什么此测试失败:预期null为“this is a party”。 我试图模仿党的服务,以返回一个虚假的价值,但它不工作Angularjs 使用jasmine正确模拟angular中的服务,angularjs,jasmine,Angularjs,Jasmine,为什么此测试失败:预期null为“this is a party”。 我试图模仿党的服务,以返回一个虚假的价值,但它不工作 'use strict'; describe("test controller: ConsultationService", function () { var scope; var controller; var partyService = { get: function() {} } beforeEach(an
'use strict';
describe("test controller: ConsultationService", function () {
var scope;
var controller;
var partyService = {
get: function() {}
}
beforeEach(angular.mock.module('aureus'));
beforeEach(function() {
module(function($provide) {
$provide.value('partyService', partyService);
});
});
beforeEach(inject(function ($rootScope, $controller, $q) {
spyOn(partyService, 'get').and.callFake(function() {
var deferred = $q.defer();
deferred.resolve('this is a party');
return deferred.promise;
});
scope = $rootScope.$new();
controller = $controller('ConsultationController', { $scope: scope, $partyService: partyService });
}));
it("scope consultation should be defined", function () {
expect(scope.consultation).toBe(null);
expect(scope.party).toBe('this is a party');
});
});
这个控制器呢
aureus.controller('ConsultationController',
['$scope', '$location', '$http', 'ngProgress', 'partyService', 'ConsultationService',
function ($scope, $location, $http, ngProgress, $partyService, consultationService) {
$scope.consultation = null;
$scope.party = null;
$scope.urlData = Api.parseUrl($location.$$absUrl, '/comments/{account}');
$partyService.get($scope.urlData.account).then(function (party) {
$scope.party = party;
});
使用延迟时,您需要在测试中运行$scope.$摘要。将此行添加到测试中:
scope.$digest()
这将导致延迟实际解析,并且您的代码将命中then回调