我目前正在更新我的popover / tooltip库的测试套件,从Jasmine 1.3到Jasmine 2。
该库以大多数异步方式运行,因为它广泛使用CSS转换。例如,
.show()
工具提示,因此,我的大部分规格都采用以下形式:
it("should update the CSS classes after repositioning the tooltip due to lack of space", function() {
var popoverIsVisible = false;
// instantiate a popover with specific settings
runs(function() {
instance = new Popover(domElement, {position: "left center", alternativePositions: ["right center"]});
instance.addCallback('afterShow', function() { popoverIsVisible = true; });
instance.show();
});
// the afterShow callback is run after the CSS transitions
// have run and the tooltip is visible
waitsFor(function() {
return popoverIsVisible;
}, "The afterShow callback should have triggered", 100);
runs(function() {
expect(instance.popover().hasClass('horizontal-right')).toBeTruthy();
});
});
在我看来,为了转换到Jasmine 2的done()
回调样式,我必须将每个规范包装在自己的describe
块中并将库设置部分移动到beforeEach
:
describe("...", function() {
beforeEach(function(done) {
var instance = new Popover(domElement, {position: "left center", alternativePositions: ["right center"]});
instance.addCallback('afterShow', function() { done(); });
instance.show();
});
it("should update the CSS classes when repositioning the tooltip due to lack of space", function() {
expect(instance.popover().hasClass('horizontal-right')).toBeTruthy();
});
});
使用一个单独的describe
块和beforeEach
每个规格对我来说感觉不对,好像我正在对抗Jasmine。我错过了什么吗?有没有更好的方法来构建我的规格?
答案 0 :(得分:2)
如果您不想为每个人describe
分别设置beforeEach
s / it
,那么您还应该能够在异步函数的回调中满足您的期望,然后在done
之后调用it
。类似的东西:
describe("...", function() {
it("should update the CSS classes when repositioning the tooltip due to lack of space", function(done) {
var instance = new Popover(domElement, {position: "left center", alternativePositions: ["right center"]});
instance.addCallback('afterShow', function() {
expect(instance.popover().hasClass('horizontal-right')).toBeTruthy();
done();
});
instance.show();
});
});
因为it
是异步的,如果jasmine没有及时调用done
回调,它将无法通过测试,因此如果从不调用异步函数的回调,测试应该适当地失败