Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 使用jasmine正确模拟angular中的服务_Angularjs_Jasmine - Fatal编程技术网

Angularjs 使用jasmine正确模拟angular中的服务

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

为什么此测试失败:预期null为“this is a party”。 我试图模仿党的服务,以返回一个虚假的价值,但它不工作

'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回调