渲染流星模板时执行的茉莉花测试方法



我正在为Meteor应用程序编写Jasmine+Velocity的单元测试。在客户端,正在执行一些方法,以便在呈现模板时为 HTMLdiv 设置类。例如:

Template.text.rendered = function () {
  Meteor.call("textCheck", function (err, status) {
    var pageState       = $('#pageState');
    if (status.text.success){
      pageState.addClass('text-success');
    }else{
      pageState.addClass('text-danger');
    }
};

我的问题是当文本模板通过 Jasmine 呈现时,我不知道如何调用该函数。我在网上搜索了很多文档,但找不到有关如何在 Jasmine 测试中调用Template.rendered的任何信息。 I am using sanjo-jasmine. 我也不明白如何在我的测试中检查该类是否已添加到div 中。有人可以帮忙吗?

尝试以下函数:

/**
 * Call the function f the first time the template will be rendered
 * And then remove the function inserted
 * @param {Template} template A meteor template
 * @param {callback} f The function to execute after the template has been rendered
 */
this.afterRendered = function(template, f) {
    // Insert our rendered function
    template._callbacks.rendered.push(function() {
        template._callbacks.rendered.pop();
        f();
    });
}

/**
 * @source http://stackoverflow.com/questions/5525071/how-to-wait-until-an-element-exists
 * @brief Wait for something to be ready before triggering a timeout
 * @param {callback} isready Function which returns true when the thing we're waiting for has happened
 * @param {callback} success Function to call when the thing is ready
 * @param {callback} error Function to call if we time out before the event becomes ready
 * @param {int} count Number of times to retry the timeout (default 300 or 6s)
 * @param {int} interval Number of milliseconds to wait between attempts (default 20ms)
 */
this.waitUntil = function (isready, success, error, count, interval) {
    if (count === undefined) {
        count = 300;
    }
    if (interval === undefined) {
        interval = 20;
    }
    if (isready()) {
        success();
        return;
    }
    // The call back isn't ready. We need to wait for it
    setTimeout(function(){
        if (!count) {
            // We have run out of retries
            if (error !== undefined) {
                error();
            }
        } else {
            // Try again
            waitUntil(isready, success, error, count -1, interval);
        }
    }, interval);
}

他们完美地完成了摩卡+速度的工作。使用 Mocha,您只需要在tests/mocha/client文件夹中的任何位置创建一个文件lib.js并将其粘贴即可。

摩卡示例(对不起,我不使用茉莉花):

describe("check something", function() {
    it ("test something", function(done) {
        afterRendered(Template.homepage, function() {
            chai.expect(...).to.not.be.undefined;
            done();
        });
        FlowRouter.go('somewhere');
    });
    it ("test something else", function(done) {
        FlowRouter.go('home');
        var waitUntilOnError = function(done) {
            return function() {
                done("Failed to wait until")
            }
        };
        var test = function() {
            return $('div').length === 1;
        }
        var maxAttempt = 200;
        var waitBetweenAttempt = 60;
        this.timeout(maxAttempt * waitBetweenAttempt * 2);
        waitUntil(test, done, waitUntilOnError, maxAttempt, waitBetweenAttempt);
    });
});

最新更新