栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Karma + AngularJS测试中加载模拟JSON文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Karma + AngularJS测试中加载模拟JSON文件

我正在使用带有角度种子的角度设置。我最终通过直接的.json固定文件和jasmine-
jquery.js解决了这个问题。其他人都提到了这个答案,但我花了一些时间才能将所有内容都放在正确的位置。我希望这可以帮助其他人。

我的文件夹中有json文件,

/test/mock
而webapp在中
/app

karma.conf.js
有这些条目(以及其他条目):

basePath: '../',files: [      ...       'test/vendor/jasmine-jquery.js',      'test/unit*.js',      // fixtures      {pattern: 'test/mock/*.json', watched: true, served: true, included: false}    ],

然后我的测试文件有:

describe('JobsCtrl', function(){var $httpBackend, createController, scope;beforeEach(inject(function ($injector, $rootScope, $controller) {    $httpBackend = $injector.get('$httpBackend');    jasmine.getJSonFixtures().fixturesPath='base/test/mock';    $httpBackend.whenGET('http://blahblahurl/resultset/').respond(        getJSonFixture('test_resultset_list.json')    );    scope = $rootScope.$new();    $controller('JobsCtrl', {'$scope': scope});}));it('should have some resultsets', function() {    $httpBackend.flush();    expect(scope.result_sets.length).toBe(59);});});

真正的把戏是

jasmine.getJSonFixtures().fixturesPath='base/test/mock';

我最初将其设置为,
test/mock
但是它需要
base
在其中。没有基础,我会得到如下错误:

Error: JSonFixture could not be loaded: /test/mock/test_resultset_list.json (status: error, message: undefined)at /Users/camd/gitspace/treeherder-ui/webapp/test/vendor/jasmine-jquery.js:295


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/616283.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号