【问题标题】:Karma + Jasmine + JSONFixtures: Cannot read property 'ajax' of undefinedKarma + Jasmine + JSONFixtures:无法读取未定义的属性“ajax”
【发布时间】:2016-01-02 05:39:20
【问题描述】:

背景:

我正在尝试为我的 javascript jsonTransformer 编写单元测试,它将 JSON-Schema 转换为项目特定的 JSON。

作为第一个测试,我想对此转换器进行黑盒测试,它接收输入 JSON 并将转换后的 JSON 与正确的 JSON 进行比较。

我使用 Karma 和 Jasmine 作为测试环境。

问题:

如何解决以下错误?

TypeError: Cannot read property 'ajax' of undefined
at jasmine.JSONFixtures.loadFixtureIntoCache_ (.../node_modules/jasmine-jquery/lib/jasmine-jquery.js:257:6)
at jasmine.JSONFixtures.getFixtureData_ (.../node_modules/jasmine-jquery/lib/jasmine-jquery.js:249:41)
at jasmine.JSONFixtures.read (.../node_modules/jasmine-jquery/lib/jasmine-jquery.js:238:12)
at jasmine.JSONFixtures.proxyCallTo_ (.../node_modules/jasmine-jquery/lib/jasmine-jquery.js:272:29)
at window.getJSONFixture (.../node_modules/jasmine-jquery/lib/jasmine-jquery.js:836:38)
at Object.<anonymous> (...test/test.js:24:8)
at Object.e [as invoke] (.../node_modules/angular/angular.min.js:39:394)
at Object.workFn (.../node_modules/angular-mocks/angular-mocks.js:2439:20)

结构:

所有依赖项都在“./node-modules/”中。
karma.config 位于“./node-modules/karma/”中。

index.html 位于“./app/”中。
JS 文件位于“./app/js/”中。

test.js 在“./test/”中。
JSON 模拟位于“./test/mock/”中。

代码:

karma.config:

basePath: '../..',

frameworks: ['jasmine'],

files: [
    'node_modules/jasmine-jquery/lib/jasmine-jquery.js',
    'node_modules/angular/angular.min.js',
    'node_modules/angular-mocks/angular-mocks.js',
    'app/js/formulargenerator.js',
    'app/js/*.js',
    'test/*.js',

    // fixtures
    {pattern: 'test/mock/*.json', watched: true, served: true, included: false}
],

test.js:

describe('jsonTransformer', function() {
	var $httpBackend, scope;

	beforeEach(inject(function ($injector, $rootScope, $controller) {
    	jasmine.getJSONFixtures().fixturesPath='base/test/mock';

		dump(jasmine.getJSONFixtures());
		dump(getJSONFixture('mock_formularSpecification.json'));

		$httpBackend = $injector.get('$httpBackend');
    	$httpBackend.whenGET('http://localhost:8080/myProject/rest/form/1').respond(
        	getJSONFixture('mock_input.json')
    	);

    	scope = $rootScope.$new();
    	$controller('jsonTransformer', {'$scope': scope});
    	dump($controller);
	}));

	var transformedJSON = getJSONFixture('mock_output.json'); //todo: transform

	it('should have transformed the input-JSON to the correct output-JSON', function() {
		$httpBackend.flush();
    	expect(transformedJSON).toBe(getJSONFixture('mock_angularFormly.json'));
	});

});

【问题讨论】:

  • 会不会是因为 jquery 没有作为测试依赖加载?
  • 是的,很遗憾这就是问题所在。我通过 npm 安装了 jquery,但我忘记在 karma.conf 文件中列出它:'node_modules/jquery/dist/jquery.min.js',

标签: javascript angularjs jasmine karma-runner karma-jasmine


【解决方案1】:

感谢 Chanthu 指出我缺少的依赖项:jquery

我通过 npm 安装了 jquery,但我忘记在 karma.conf 文件中列出它:'node_modules/jquery/dist/jquery.min.js',

【讨论】:

    猜你喜欢
    • 2018-02-02
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    相关资源
    最近更新 更多