2016-03-01 2 views
0

Я пишу модульные тесты в Jasmine + Velocity для приложения Meteor. На стороне клиента выполняются некоторые методы для установки классов для HTML-разделов при визуализации шаблона. Например:Метод испытания Жасмина, который выполняется при создании метеорного шаблона

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

Моя проблема, я не знаю, как вызвать функцию, когда шаблон текста визуализируется через Жасмин. Я искал много документации в Интернете, но не смог найти информацию о том, как позвонить 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); 
} 

Они прекрасно делают работу с мокко + Velocity. С Mocha вам просто нужно создать файл lib.js в любом месте папки tests/mocha/client и вставить его.

Мокко пример (жаль, что я не использую жасмин):

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

 Смежные вопросы

  • Нет связанных вопросов^_^