我在我的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同步加载文件,但得到的是HTTP404。文件在哪里?
//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发布于 2015-07-25 07:27:50
在我看来,您正在尝试加载带有GET请求的本地文件;根据运行服务器的端口或主机,它可能无法通过相对路径加载它。请尝试将其指向包括协议和域名的完整URL路径,例如:
//Synchronously GET the test data
var req = new XMLHttpRequest();
req.open('GET', 'http://servername.com/app/data/file.json', false);
req.send(null);
var testData = JSON.parse(req.responseText);https://stackoverflow.com/questions/31621128
复制相似问题