[英]Loading external file from Karma/Jasmine test
我正在嘗試完成Jasmine測試(使用Karma和IntelliJ 13)來驗證JSON文件。 理想情況下,我的測試只是將JSON文件加載到數據對象中,然后讓我解析以檢查有效的格式和數據。 我不需要在之前或之后驗證函數,也不需要針對服務器進行測試。
我的基本設置是這樣的:
it("should load an external file", function(){
var asyncCallComplete, result,
_this = this;
// asyncCallComplete is set to true when the ajax call is complete
asyncCallComplete = false;
// result stores the result of the successful ajax call
result = null;
// SECTION 1 - call asynchronous function
runs(function() {
return $.ajax('/test/config.json', {
type: 'GET',
success: function(data) {
asyncCallComplete = true;
result = data;
},
error: function() {
asyncCallComplete = true;
}
});
});
// SECTION 2 - wait for the asynchronous call to complete
waitsFor(function() {
return asyncCallComplete !== false;
}, "async to complete");
// SECTION 3 - perform tests
return runs(function() {
return expect(result).not.toBeNull();
});
}
問題是,無論我使用什么路徑,我都會收到404錯誤,文件將無法加載。 我嘗試使用此測試服務從遠程服務器加載外部JSON結果:
這很有效。
我的測試文件名為/test/mySpec.js,我的karma.conf.js文件位於根目錄下。 我已經將JSON文件移動到所有這些位置而沒有運氣。 我究竟做錯了什么?
更新與答案:
根據下面的答案,我將此添加到我的karma.conf.js:
// fixtures
{ pattern: 'test/*.json',
watched: true,
served: true,
included: false
}
然后,我這樣寫了我的測試:
var json:any;
it("should load a fixture", function () {
jasmine.getFixtures().fixturesPath = "base/test/"
var f = readFixtures("registration.json");
json = JSON.parse(f);
expect(json).toBeDefined();
})
it("should have a title", function () {
expect(json.title).toNotBe(null);
})
etc...
它過去了。
您是否通過karma.config.js提供JSON文件?
您可以通過fixture提供JSON文件:
files: [
// angular
'angular.min.js',
'angular-route.js',
'angular-mocks.js',
// jasmine jquery helper
'jquery-1.10.2.min.js',
'jasmine-jquery.js',
// app
'../../public/js/app.js',
// tests
'*-spec.js',
// JSON fixture
{ pattern: '/test/*.json',
watched: true,
served: true,
included: false }
],
通過夾具提供JSON是最簡單的,但由於我們的設置,我們不能輕易做到這一點,所以我寫了一個替代的輔助函數:
bower install karma-read-json
將karma-read-json.js放入您的Karma文件中,例如:
files = [ ... 'bower_components/karma-read-json/karma-read-json.js', ... ]
確保您的JSON由Karma提供,例如:
files = [ ... {pattern: 'json/**/*.json', included: false}, ... ]
在測試中使用readJSON
函數。 例:
var valid_respond = readJSON('json/foobar.json'); $httpBackend.whenGET(/.*/).respond(valid_respond);
如果您嘗試加載HTML文件並希望避免使用jasmine-jquery ,則可以利用karma-ng-html2js-preprocessor 。
在你的karma.conf.js中 :
// generate js files from html templates
preprocessors: {
'resources/*.html': 'ng-html2js'
},
files: [
...
'resources/*.html'
],
plugins: [
...
'karma-ng-html2js-preprocessor'
],
在你的茉莉花規格:
beforeEach(module('resources/fragment.html'));
var $templateCache;
beforeEach(inject(function (_$templateCache_) {
$templateCache = _$templateCache_;
}));
describe('some test', function () {
it('should do something', function () {
// --> load the fragment.html content from the template cache <--
var fragment = $templateCache.get('resources/fragment.html');
expect(fragment).toBe(...);
});
});
您是否嘗試過只需要json文件並將其作為全局變量存儲在測試中?
我正在開發一個Angular2項目(使用Angular CLI),並且使用此設置它可以工作:
// On the very beginning of the file let mockConfig = require('./test/config.json');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.