我正在尝试使用jasmine在单元测试中模拟XMLHttpRequest。我使用Aurelia来生成我的应用程序,我使用Karma作为单元测试运行程序。
对于单元测试,我尝试导入jasmine:
import * as jasmine from 'jasmine-ajax';
describe('when calling the API', () => {
beforeEach(() => {
jasmine.Ajax.install();
});
afterEach(() => {
jasmine.Ajax.uninstall();
});
it('then it should get an answer back', () => {
var doneFn = jasmine.createSpy("success");
var xhr= new XMLHttpRequest();
xhr.onreadystatechange = function(args) {
if(this.readyState == this.DONE) {
doneFn(this.responseText);
}
};
xhr.open("GET", "https://someaddress.net/api/plans");
xhr.send();
expect(jasmine.Ajax.requests.mostRecent().url).toBe("https://someaddress.net/api/plans");
expect(doneFn).not.toHaveBeenCalled();
jasmine.Ajax.requests.mostRecent().respondWith({
"status":200,
"contentType": 'text/plain',
"responseText": 'awesome response'
});
expect(doneFn).toHaveBeenCalledWith('awesome response');
});
});在使用Karma进行测试时,我得到:
jasmine_ajax__WEBPACK_IMPORTED_MODULE___.jasmine :在测试/业力-bundle.js行3146 >TypeError(第7行)中未定义
我发现我已经安装了jasmine的类型定义。
npm @type/jasmine
然后,我删除了jasmine的import语句,这导致了以下错误:
TypeError:在测试/业力-bundle.js第3134行> jasmine.Ajax (第3行)中未定义jasmine.Ajax
那我做错什么了?
我用茉莉花3.3.9,茉莉花-ajax 3.4.0,业力4.0.1,打字本2.9.2,webpack 4.25,
发布于 2019-03-18 11:26:34
在安装了业力-jasmine (https://github.com/IDCubed/karma-jasmine-ajax)并在karma.conf.js中的框架数组中添加了“jasmine”之后,它就开始工作了。
module.exports = function(config) {
config.set({
frameworks: ['jasmine-ajax', 'jasmine']
});
}https://stackoverflow.com/questions/55183168
复制相似问题