Angular karma-jasmine:XHR / GET

时间:2015-07-24 23:13:06

标签: angularjs node.js karma-runner karma-jasmine

我在karma.conf.js文件数组中包含了一个现有的项目文件:

files : [
  'app/bower_components/angular/angular.js',
  'app/bower_components/angular-route/angular-route.js',
  'app/bower_components/angular-mocks/angular-mocks.js',
  'app/js/**/*.js',
  'test/unit/**/*.js',
  'bower.json',
  'app/data/file.json',
],

我知道该文件已匹配,因为我没有得到警告 WARN [watcher]: Pattern <pattern> does not match any file.我将该行更改为已知的不存在的文件,然后再进行双重检查。

我的基本路径是项目根目录:

basePath : '../',

当Angular的controllersSpec.js运行时,我使用XHR GET同步加载文件,但是我得到了一个HTTP 404.文件在哪里?

//Synchronously GET the test data
var req = new XMLHttpRequest();
req.open('GET', 'app/data/file.json', false);
req.send(null);

var testData = JSON.parse(req.responseText);

然后,在测试shell中,我看到:

WARN [web-server]: 404: /app/data/file.json
<user agent> ERROR
  SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
  at <path to project>/test/unit/controllersSpec.js:12

1 个答案:

答案 0 :(得分:1)

在我看来,您正在尝试使用GET请求加载本地文件;根据您的服务器运行的端口或主机,它可能无法通过相对路径加载它。请尝试将其指向包含协议和域名的完整URL路径,例如:

{{1}}