2016-03-01 26 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); 
    }); 
}); 
相關問題