我目前正在更新我的弹出/工具提示库的测试套件,从Jasminin1.3升级到Jasminine2。
该库主要以异步方式运行,因为它广泛使用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对我来说都是错误的,就好像我在对付茉莉花一样。我是不是遗漏了什么?有更好的方法来构造我的规格吗?
发布于 2014-02-24 04:11:07
如果您不希望为每个单独的describe/beforeEach设置单独的it,那么您还应该能够在异步函数的回调中得到您的期望,并在此之后为it调用done。类似于:
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是异步的,如果没有及时调用done回调,茉莉花就会失败,因此,如果从未调用来自异步函数的回调,那么您的测试就会适当地失败。
https://stackoverflow.com/questions/21911635
复制相似问题