Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 呈现Meteor模板时执行的Jasmine测试方法_Javascript_Unit Testing_Meteor_Jasmine_Client Side - Fatal编程技术网

Javascript 呈现Meteor模板时执行的Jasmine测试方法

Javascript 呈现Meteor模板时执行的Jasmine测试方法,javascript,unit-testing,meteor,jasmine,client-side,Javascript,Unit Testing,Meteor,Jasmine,Client Side,我正在用Jasmine+Velocity为Meteor应用程序编写单元测试。在客户端,当呈现模板时,执行一些方法来设置HTML div的类。例如: Template.text.rendered = function () { Meteor.call("textCheck", function (err, status) { var pageState = $('#pageState'); if (status.text.success){ pag

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

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
的任何信息<代码>我正在使用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);
}
他们用摩卡+Velocity完美地完成了这项工作。使用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);
    });
});