gpt4 book ai didi

javascript - 使用 Squire.js 注入(inject)依赖项时未执行 Jasmine 规范

转载 作者:行者123 更新时间:2023-11-29 15:39:56 24 4
gpt4 key购买 nike

我正在尝试为 Backbone View 运行一些 Jasmine 单元测试,模拟 Squire 中的依赖项。

我的 View 的依赖项是一个 Baseview、一个 ICanHaz 模板和一个 i18n 翻译。

我在定义 Squire 和 Backbone 之后模拟出依赖关系,然后使用 Squire 注入(inject)器来请求我的 View 。但是,当我通过 Grunt 运行测试时,我收到警告消息:

Warning: No specs executed, is there a configuration error? Use --force to continue.

这是我的规范:

define(['squire', 'backbone'], function (Squire, Backbone) {
var injector = new Squire();

mocks = {
'views/baseview': function () {
return Backbone.View.extend({
grabTemplate: function (options) { }
});
},
'text!templates/menu.htm': '',
'i18n!nls/menu': {}
};

injector.mock(mocks);

injector.require(['menu'], function (Menu) {

describe('Menu View', function () {

it('should be initialisable', function () {
var menu = new Menu();
expect(menu).toBeDefined();
});
});
});
});

有谁知道为什么我的基本单元测试没有被接受?

最佳答案

经过一些尝试和错误,我找到了一个让我隐隐约约感到满意的解决方案。我从 this post 修改了解决方案,利用 Jasmine 的 runwaitsFor 调用:

define(['squire', 'backbone'], function (Squire, Backbone) {

describe('Menu View module', function () {

it('should be initialisable', function () {

var injector = new Squire(),
mocks,
menu,
loaded = false; //track whether our module has loaded

mocks = {
'views/baseview': function () {
return Backbone.View.extend({
grabTemplate: function (options) { }
});
},
'text!templates/menu.htm': '',
'i18n!nls/menu': {}
};

injector.mock(mocks);

injector.require(['views/menu'], function (Menu) {
menu = new Menu();
loaded = true;
});

waitsFor(function() {
return loaded;
//when this is true, we'll drop out of waitsFor
}, 'Menu module not loaded', 10000);

runs(function() {
expect(menu).toBeDefined();
});
});
});
});

关于javascript - 使用 Squire.js 注入(inject)依赖项时未执行 Jasmine 规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21017252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com