背景:
我试图为我的javascript jsonTransformer编写单元测试,它将JSON转换为特定的JSON。
作为第一个测试,我希望对这个转换器进行一个黑盒测试,它接收输入- JSON,并将转换后的JSON与正确的JSON进行比较。
我用卡玛和茉莉花作为测试环境。
问题:
如何解决以下错误?
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/”中。
karma.config在“./节点-模块/业力/”中。
index.html在"./app/“中。
在"./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'));
});
});
发布于 2016-01-02 15:13:43
感谢Chanthu指出我缺少的依赖关系: jquery
我通过npm安装了jquery,但忘记将它列在karma.conf文件中:‘node_node/jquery/dist/jquery.min.js’,
https://stackoverflow.com/questions/34562990
复制相似问题