Предыстория:
Я пытаюсь написать модульные тесты для моего 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/".
Код:
карма.конфиг:
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'));
});
});