1

我有一个角度应用程序,用户可以从他们的保管箱帐户中选择一个文件,然后应用程序下载该文件。这是代码。

$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');
    });
});
4

0 回答 0