简体   繁体   English

茉莉花独立中的辅助函数

[英]helper functions in jasmine standalone

I have a Spec file unit testing a bunch of methods.我有一个 Spec 文件单元测试了一堆方法。 In the same file I have helper functions.在同一个文件中,我有帮助函数。 Is there a way to move the helper functions to a separate file with jasmine standalone and vanilla js?有没有办法将辅助函数移动到单独的 jasmine 独立文件和 vanilla js 文件中?

  describe('#formatDateAndTime', () => {
    it('formats the date and time as per the requirements', () => {
      expect(till._formatDateAndTime()).toEqual(timeAndDate());
    });
  });

  function timeAndDate () {
    let today = new Date();
    return [today.getFullYear() + '.' + (today.getMonth() + 1) + '.' +
    today.getDate() + ' ' + today.getHours() + ':' + today.getMinutes() +
    ':' + today.getSeconds()];
  }

how can I move timeAndDate() function to a helper file如何将 timeAndDate() 函数移动到帮助文件

With Jasmine standalone, you link to all your scripts in an html file — let's call that tests.html.使用 Jasmine Standalone,您可以链接到 html 文件中的所有脚本——我们称之为 tests.html。 Let's say your test cases are in Tests.js, and you put your helpers in TestUtils.js.假设您的测试用例在 Tests.js 中,而您将助手放在 TestUtils.js 中。

Here's what you can do in that HTML file:您可以在该 HTML 文件中执行以下操作:

tests.html测试.html

 <html> <head> <title>Tests</title> <!-- Jasmine --> <link rel="shortcut icon" type="image/png" href="jasmine/lib/jasmine-3.3.0/jasmine_favicon.png"> <link rel="stylesheet" type="text/css" href="jasmine/lib/jasmine-3.3.0/jasmine.css"> <script type="text/javascript" src="jasmine/lib/jasmine-3.3.0/jasmine.js"></script> <script type="text/javascript" src="jasmine/lib/jasmine-3.3.0/jasmine-html.js"></script> <script type="text/javascript" src="jasmine/lib/jasmine-3.3.0/boot.js"></script> <!-- All scripts you want to test --> <script src='MyCoolProgram.js'></script> <!-- Specs --> <script src='TestUtils.js'></script> <script src='Tests.js'></script> </head> </html>

Then, simply open that file in a browser, and you should be able to see your tests running as required.然后,只需在浏览器中打开该文件,您就应该能够看到您的测试按要求运行。

Careful!小心! You must put TestUtils.js before Tests.js if you want Tests.js to call TestUtils.js helper methods.如果您希望 Tests.js 调用 TestUtils.js 辅助方法,则必须将 TestUtils.js 放在 Tests.js之前 This is because in HTML, statically included scripts are injected in the order they appear in the file.这是因为在 HTML 中,静态包含的脚本是按照它们在文件中出现的顺序注入的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM