Jasmine specs没有specRunner.html - JS依赖关系 [英] Jasmine specs without specRunner.html - JS dependencies

查看:104
本文介绍了Jasmine specs没有specRunner.html - JS依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序的单元测试,其中我想测试的JavaScript依赖于其他JS框架(underscore.js,backbone.js,jQuery ...)。因此,在设置specRunner.html时,我还添加了这些文件:

  .... 
< head> ;
< title> p13n.js Spec Runner< / title>

< link rel =shortcut icontype =image / pnghref =./ jasmine-standalone-1.3.1 / lib / jasmine-1.3.1 / jasmine_favicon.png> ;
< link rel =stylesheettype =text / csshref =./ jasmine-standalone-1.3.1 / lib / jasmine-1.3.1 / jasmine.css>
< script src =./ jasmine-standalone-1.3.1 / lib / jasmine-1.3.1 / jasmine.js>< / script>
< script src =./ jasmine-standalone-1.3.1 / lib / jasmine-1.3.1 / jasmine-html.js>< / script>

<! - 包括此处的源文件... - >
< script src =... / jquery.min.js>< / script>
< script src =... / underscore-min.js>< / script>
< script src =... / backbone-min.js>< / script>
< script src =... / MyApplication-file.js>< / script>

<! - 包括这里的规格文件... - >
< script src =... / spec / MyApplication-spec.js>< / script>

< script type =text / javascript>
(function(){
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;

var htmlReporter = new jasmine.HtmlReporter();
...

然而,这不适用于使用像Grunt这样的工具进行自动化测试。我不能依靠specRunner.html加载我的依赖项,因为.html没有被使用。你如何管理依赖项?


  • 我应该测试我的应用程序代码+供应商的缩小版本吗?

  • 我应该在单元测试规范中使用像require.js这样的工具吗?

  • Grunt解决这个问题的另一种方法是什么?


解决方案

您使用的IDE或编辑器可以用与添加对文件引用来获得代码完成的相同方式来指定依赖项。

例如,在Visual Studio中,使用的格式是:

  ///< reference path =〜/ li b / jasmine-2.0.0 / jasmine.js/> 
///< reference path =〜/ lib / jasmine-2.0.0 / jasmine-html.js/>
///< reference path =〜/ lib / jasmine-2.0.0 / console.js/>
///< reference path =〜/ lib / jasmine-2.0.0 / boot.js/>

///< reference path =../../../../& ///< reference path =../../../../& lt; PorjectName> /bundles/app.javascript.js/>

其中< ProjectName> 是您的项目名称,仅此因为测试项目是与主应用程序
不同的项目

I have unit tests for an application where the javascript I wanted to test was dependent on other JS frameworks (underscore.js, backbone.js, jQuery...). So when setting up the specRunner.html I added these files as well:

....
<head>
  <title>p13n.js Spec Runner</title>

  <link rel="shortcut icon" type="image/png" href="./jasmine-standalone-1.3.1/lib/jasmine-1.3.1/jasmine_favicon.png">
  <link rel="stylesheet" type="text/css" href="./jasmine-standalone-1.3.1/lib/jasmine-1.3.1/jasmine.css">
  <script src="./jasmine-standalone-1.3.1/lib/jasmine-1.3.1/jasmine.js"></script>
  <script src="./jasmine-standalone-1.3.1/lib/jasmine-1.3.1/jasmine-html.js"></script>

  <!-- include source files here... -->
  <script src=".../jquery.min.js" ></script>
  <script src=".../underscore-min.js"></script>
  <script src=".../backbone-min.js"></script>
  <script src=".../MyApplication-file.js"></script>

  <!-- include spec files here... -->
  <script src=".../spec/MyApplication-spec.js"></script>

  <script type="text/javascript">
    (function() {
      var jasmineEnv = jasmine.getEnv();
      jasmineEnv.updateInterval = 1000;

      var htmlReporter = new jasmine.HtmlReporter();
...

However, this does not work for automated testing using a tool like Grunt. I can't rely on the the specRunner.html to load my dependencies, as the .html is not used. How do you manage dependencies?

  • should I be testing a minified and compressed version of my app code + vendor?
  • should I be using a tool like require.js within my unit test spec?
  • does Grunt solve for this another way?

解决方案

Depending on your IDE or Editor you are using you can specify dependencies in the same way you add references to files to get code completion.

E.g. in Visual Studio for example the format used is:

/// <reference path="~/lib/jasmine-2.0.0/jasmine.js" />
/// <reference path="~/lib/jasmine-2.0.0/jasmine-html.js" />
/// <reference path="~/lib/jasmine-2.0.0/console.js" />
/// <reference path="~/lib/jasmine-2.0.0/boot.js" />

/// <reference path="../../../../<ProjectName>/javascript/shared/jquery-1.7.2.min.js" />
/// <reference path="../../../../<PorjectName>/bundles/app.javascript.js" />

Where <ProjectName> is your project name and this is only the case as the Test Project is a separate Project from the main App

这篇关于Jasmine specs没有specRunner.html - JS依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆