我如何测试$ broadcast的广播是$ http post方法

时间:2016-02-16 22:25:37

标签: angularjs jasmine karma-jasmine

以下是我服务类中的代码。

$timeout(function() {
    $rootScope.$broadcast('transfer:fileTransferInProgress');
    debugger 
    $http.post("/api/fileTransfer", data, {
        transformRequest: angular.identity,
        headers: {'Content-Type': undefined}                   
    }).success(function(response){
        console.log("I am here");
        $rootScope.$broadcast('transfer:readyState', response);
    }).error(function(){
        $rootScope.$broadcast('transfer:error');
    });
}, 0);

我正在尝试下面的代码来测试$broadcast

'use strict';

describe('upload service test ', function () {

    var mockXhr, uploadSvc;
    var $scope, $rootScope, $mockHttp, $httpBackend,$timeout;
    var mockfile = {"name":"File 1", "body":"abcd121212"}; 
    var mockfileType = {"id":"1"}; 
    var mockfileTypeName="test";

    beforeEach(module('questApp'));

    it('should invoke send method of upload service with right paramaeters', function () {

        inject(function($injector) {
            $rootScope = $injector.get('$rootScope');
            // $scope = $rootScope.$new();
            $mockHttp = $injector.get('$http');            
            uploadSvc=$injector.get('uploadService');
            $httpBackend = $injector.get('$httpBackend');
            spyOn($rootScope, '$broadcast');
            $timeout = $injector.get('$timeout');
        })
        $rootScope.globals = {
            currentUser : function() {
                return {
                    username : "admin"
                };
            }
        };
        $httpBackend.expectPOST('/api/fileTransfer', {
            'file':mockfile ,
            'fileType':mockfileType,
            'fileTypeName'  :mockfileTypeName   
        }).respond({});
        debugger 
        uploadSvc.send('file','fileType','fileTypeName'); 
        $timeout.flush();
        expect($rootScope.$broadcast).toHaveBeenCalled();
    });
});

我不确定有什么不对,但它给了我以下错误。

  

错误:意外请求:POST   / api / fileTransfer?cacheBuster = 1455661017032预期的POST   / API /文件传输

0 个答案:

没有答案