渲染Meteor模板时执行的Jasmine测试方法

时间:2016-03-01 12:17:36

标签: 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){
      pageState.addClass('text-success');
    }else{
      pageState.addClass('text-danger');
    }
};

我的问题是,当通过Jasmine呈现文本模板时,我不知道如何调用该函数。我在网上搜索了很多文档但是找不到有关如何在Jasmine测试中调用Template.rendered的任何信息。 I am using sanjo-jasmine.此外,我无法理解如何检查我的测试是否已将类添加到div中。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

试试这些功能:

<!-- first, add the binary extension.  Be sure to specify the source file as WixDacPacExtension.CA.dll. -->
<Binary
     Id="WixDacPacExtensionBinary"
     SourceFile="<Path to your file>\WixDacPacExtension.CA.dll"/>

<!-- Create a custom action to run first and set up all the parameters that are -->
<!-- passed to the Wix DacPac Extension.  The property name MUST MATCH -->
<!-- the name of the custom action that executes the binary defined above. -->
<!-- The parameters in the Value property are semi-colon delimited. -->
<CustomAction
     Id="SetupDacPacWIXDacPacInstallerExampleCustomAction"
     Property="DacPacWIXDacPacInstallerExampleCustomAction" 
     Value="ShowUI=True;SqlPackagePath=c:\Program Files (x86)\Microsoft SQL Server\120\DAC\bin\SqlPackage.exe;DacPacPath=[INSTALLFOLDER]WIXDacPacInstallerExample.dacpac;LogFilePath=[TempFolder]\WIXDacPacInstallerExample.dacpac.log;TargetServerName=[DATABASESERVER];TargetDatabaseName=WIXDacPacInstallerExample;OtherParameters=/p:RegisterDataTierApplication=True /p:BlockWhenDriftDetected=False /p:BlockOnPossibleDataLoss=False"
        />

<!-- 
     This custom action will execute the extension with the parameters from Step #1.
     NOTE: the Id of this custom action matches the Property of the custom action
          from Step #1.
-->
<CustomAction
     Id="DacPacWIXDacPacInstallerExampleCustomAction"
     BinaryKey="WixDacPacExtensionBinary"
     DllEntry="Execute"
     Execute="deferred"
     Return="check"
/>

他们完美地使用Mocha + Velocity完成工作。使用Mocha,您只需在/** * 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); } 文件夹中的任意位置创建文件lib.js并粘贴即可。

摩卡示例(抱歉,我不使用茉莉花):

tests/mocha/client