2016-03-01 68 views
0

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

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或不。有人可以帮忙吗?

回答

0

试用一下这些功能:

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

他们是完全做的工作与摩卡+速度。借助摩卡,您只需在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); 
    }); 
});