Jasmine 如何测试和模拟返回arraybuffer的xmlhttprequest

Jasmine 如何测试和模拟返回arraybuffer的xmlhttprequest,jasmine,jasmine-ajax,Jasmine,Jasmine Ajax,我有一个angular应用程序,用户可以从dropbox帐户中选择一个文件,然后应用程序下载该文件。这是密码 $scope.$watchCollection('dropBoxFiles', function (newDBFiles) { $scope.invalidResume = false; if (newDBFiles.length > 0) { var mimeType = $scope.validateFile(newDBFiles[newDBFi

我有一个angular应用程序,用户可以从dropbox帐户中选择一个文件,然后应用程序下载该文件。这是密码

$scope.$watchCollection('dropBoxFiles', function (newDBFiles) {
    $scope.invalidResume = false;
    if (newDBFiles.length > 0) {
        var mimeType = $scope.validateFile(newDBFiles[newDBFiles.length - 1].name);
        if (mimeType === ''){
            return false;
        }
        var remote = new XMLHttpRequest();
        remote.open('GET', newDBFiles[newDBFiles.length - 1].link);
        remote.responseType = "arraybuffer";
        remote.onload = function() {
            $scope.filename = newDBFiles[newDBFiles.length - 1].name;
            $scope.fileBlob = new Blob([remote.response], {type: mimeType});
            $scope.$digest();
        };
        remote.send();
    }
});
我正在努力编写一个涵盖xmlhttprequest部分的测试。这是我到目前为止得到的,但我得到了这个错误-

TypeError: 'undefined' is not an object (evaluating 'request.respondWith')
我知道这个错误意味着什么,但我不确定为什么我会得到它。这是测试

describe('dropbox watch collection ', function () {

    var testResponses = {
        results: {
            success: {
                status: 200,
                responseText: 'test text',
                response: '<html><body>hello</body></html>'
            }
        }
    };

    beforeEach(inject(function ($rootScope, $controller) {
        jasmine.Ajax.install();
        scope = $rootScope.$new();
        $controller('ApplyController', { $scope: scope});
    }));

    afterEach(function() {
        jasmine.Ajax.uninstall();
    });

    it('sets the scope filename to selected file if using a valid file type', function () {
        var selectedFile = {
            name: 'test.html',
            link: 'http://dropbox.com',
            mimeType: 'text/html'
        };

        scope.dropBoxFiles.push(selectedFile);
        scope.$digest();

        var request = jasmine.Ajax.requests.mostRecent();
        request.respondWith(testResponses.results.success);

        expect(scope.filename).toBe(selectedFile.name);
        expect(typeof scope.fileBlob).toBe('blob');
    });
});
description('dropbox watch collection',函数(){
var testResponses={
结果:{
成功:{
现状:200,
responseText:“测试文本”,
回答:“你好”
}
}
};
beforeach(注入(函数($rootScope,$controller){
jasmine.Ajax.install();
scope=$rootScope.$new();
$controller('ApplyController',{$scope:scope});
}));
之后(函数(){
jasmine.Ajax.uninstall();
});
它('如果使用有效的文件类型,则将作用域文件名设置为选定文件',函数(){
var selectedFile={
名称:“test.html”,
链接:'http://dropbox.com',
mimeType:'text/html'
};
scope.dropBoxFiles.push(selectedFile);
范围。$digest();
var request=jasmine.Ajax.requests.mostRecent();
request.respondWith(testResponses.results.success);
expect(scope.filename).toBe(selectedFile.name);
expect(type of scope.fileBlob).toBe('blob');
});
});