Javascript 为什么Karma拒绝为我的JSON装置服务(我想在jasmine/angularjs测试中使用它)

Javascript 为什么Karma拒绝为我的JSON装置服务(我想在jasmine/angularjs测试中使用它),javascript,json,jasmine,karma-runner,Javascript,Json,Jasmine,Karma Runner,正如这篇文章所指出的,看起来业力将为你服务。然而,我花了太多时间试图让它在我的环境中工作。原因:我正在进行角度测试,需要将模拟HTTP结果加载到测试中,因为Jasmine不支持模拟服务器和其他东西的任何全局设置/拆卸 在我的karma配置文件中,我将设备定义为: files: [ // angular 'angular/angular.min.js', 'angular/angular-route.js', 'angular/mock/angular-mocks.js',

正如这篇文章所指出的,看起来业力将为你服务。然而,我花了太多时间试图让它在我的环境中工作。原因:我正在进行角度测试,需要将模拟HTTP结果加载到测试中,因为Jasmine不支持模拟服务器和其他东西的任何全局设置/拆卸

在我的karma配置文件中,我将设备定义为:

files: [
  // angular 
  'angular/angular.min.js',
  'angular/angular-route.js',
  'angular/mock/angular-mocks.js',

  // jasmine jquery helper
 'jquery-1.10.2.min.js',
 'angular/jasmine-jquery.js',

  // our app
  '../public/js/FooApp.js',

  // our tests
  'angular/*-spec.js',

  // fixtures
  { pattern: 'node/mock/factoryResults.json',
    watched: 'true',
    served:  'true',
    included: 'false' }
]
在我尝试在jasmine测试中使用jasmine-jquery.js加载JSON之前,我看到karma在尝试为它服务时哽住了:

...
DEBUG [web-server]: serving: /Users/XXX/FooApp/spec/node/mock/factoryResults.json
Firefox 25.0.0 (Mac OS X 10.8) ERROR
    SyntaxError: missing ; before statement
    at /Users/XXX/FooApp/spec/node/mock/factoryResults.json:1
...
以下是factoryResults.json的外观:

{ "why": "WHY" }

知道这是怎么回事吗?我在网上看到很多人通过karma fixture成功地将JSON加载到jasmine测试中的例子。因果报应可以看到文件;如果我在fixture块中放置了错误的路径,我会看到一个错误,指出它找不到任何与我的fixture模式匹配的文件。我尝试过用不同的方式重新格式化.json文件。。。有什么想法吗?

你的问题是
'false'
必须是布尔值,而不是字符串

已经有很多方法可以更好地验证配置并修复此类错误


此外,您还可以编写一个简单的“json”预处理器(类似于),使其成为有效的JS,并将json放入某个全局名称空间,以便您可以保持测试同步…

我的karma测试套件中也需要json装置。 最后,我将html2js预处理器与json文件以及html一起使用

karma.conf.js:

module.exports = function (config) {
  config.set({
    frameworks: ["jasmine"],
    files: [
        '**/*.js',
        '**/*.html',
        '**/*.json',
        '**/*.spec.js'
    ],
    plugins: [
        'karma-html2js-preprocessor'
    ]
    preprocessors: {
        '**/*.html': ['html2js'],
        '**/*.json': ['html2js']
    }
  });
};
然后,只需从_uhtml__;全局获取json即可

e、 g


因此,我对jasmine jquery有很多问题,我得到了一个相当不错的解决方法

这有点老套,但很管用。基本上,我只是在窗口上创建一个可访问的函数,然后将JSON装置堆叠在一个小开关中:

if (typeof(window.fixtures === "undefined")) {
  window.fixtures = {};
}

window.setFixture = function(type) {
  var json;

  if (type == "catalog") {
    json = { ... }
  }

if (typeof(type) !== "undefined") {
  window.fixtures[type] = json;
}
  return json;

}
然后,我可以将其插入视图中:

describe "App.Models.Catalog", ->
  it "provides the 'App.Models.Catalog' function", ->
    expect(App.Models.Catalog).toEqual(jasmine.any(Function))

  it "sets up a fixture", ->
    setFixture("catalog")
    console.log(fixtures["catalog"])
    expect(fixtures["catalog"]).toBeDefined()
起重臂,测试通过,对象出现在日志中:

{
  catalog_id: '2212',
  merchant_id: '114',
  legacy_catalog_id: '2340',
  name: 'Sample Catalog',
  status: '1',
  description: 'Catalog Description ',
}
现在,它可以在我的测试中访问

这当然不是完美的,也不是理想的,但我一直在用
jasmine jquery
插件遇到奇怪的
matchErrors
之类的问题,而且它足够简单(也很快),我可以粘贴几个JSON块并开始移动

您还可以节省自己的时间来摆弄配置,并为Karma对
文件进行任何更改


有谁有更好的建议或者有没有运气让
jasmine jquery
工作?

我能够让
jasmine jquery
使用最新版本。具有讽刺意味的是,他们的方法与我上面提出的小黑客方法几乎完全相同。我建议您研究一下,而不要尝试使用任何其他库。
{
  catalog_id: '2212',
  merchant_id: '114',
  legacy_catalog_id: '2340',
  name: 'Sample Catalog',
  status: '1',
  description: 'Catalog Description ',
}