【问题标题】:Jasmine test method executed when Meteor template is rendered渲染 Meteor 模板时执行的 Jasmine 测试方法
【发布时间】:2016-03-02 00:45:52
【问题描述】:

我正在用 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 的信息。 I am using sanjo-jasmine. 另外,如果该类已添加到 div 中,我不明白如何检查我的测试。有人可以帮忙吗?

【问题讨论】:

    标签: javascript unit-testing meteor jasmine client-side


    【解决方案1】:

    试试这些功能:

    /**
     * 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 + Velocity 完美地完成了这项工作。使用 Mocha,您只需在 tests/mocha/client 文件夹的任意位置创建一个文件 lib.js 并粘贴即可。

    Mocha 示例(对不起,我没有使用 jasmine):

    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);
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-12
      • 2015-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-11
      • 2014-09-22
      相关资源
      最近更新 更多