4

我正在使用 java 来运行 Envjs,以便在 Jasmine 中运行 javascript 单元测试。这将允许我在没有浏览器的情况下运行测试,并且更容易集成到 Jenkins(一个持续集成构建服务器)中。

我有一个 LoadSpecRunner.js 文件(Envjs 运行),它使用如下代码加载实际的 jasmine 测试运行器。

window.location.href = 'file:///c:/source/JasmineTest/SpecRunner.html');

问题是设置文件的完整 url 工作正常,而我设置相对路径的所有尝试都失败了。以下是我在返回的输出中设置相对 url 的一些尝试

window.location.href = Envjs.uri('../JasmineTest/SpecRunner.html');

或者

window.location.href = '../JasmineTest/SpecRunner.html';

无法打开文件 file://c/source/JasmineTest/SpecRunner.html
Java 异常:java.net.UnknownHostException:c

window.location.href = window.location.href;

无法打开文件 file://c/:/Source/jasmine-reporters/about:blank
JavaException: java.net.UnknownHostException: c

有人有什么想法吗?

谢谢

切德

PS。进一步阅读我正在尝试做的事情:

http://skaug.no/ingvald/2010/10/javascript_unit_testing/

http://www.build-doctor.com/2010/12/08/javascript-bdd-jasmine/

4

2 回答 2

1

我遇到了同样的问题并通过修改 env.rhino.1.2.js解决了它:

if (!base) {
    base = 'file://' +  Envjs.getcwd() + '/';
}

->

if (!base) {
    base = 'file:///' +  Envjs.getcwd() + '/';
}
于 2014-01-26T10:07:29.213 回答
0

希望我正确理解了您的查询 - 下面看起来如何?(如果我稍微偏离了一点,您可能需要调整 baseURL)。

功能;

function resolvePath (relativePath) {
  var protocol = "file:///c:/";
  var baseUrl = "source/JasmineTest";
  var reUpward = /\.\.\//g;
  var upwardCount = (relativePath.match(reUpward) || []).length;
  return protocol + (!upwardCount ? baseUrl : baseUrl.split("/").slice(0, -upwardCount).join("/")) + "/" + relativePath.replace(reUpward, "");
}

示例调用;

resolvePath("SpecRunner.html");
// "file:///c:/source/JasmineTest/SpecRunner.html"
resolvePath("path/SpecRunner.html");
// "file:///c:/source/JasmineTest/path/SpecRunner.html"
resolvePath("../../SpecRunner.html");
// "file:///c://SpecRunner.html"
resolvePath("../SpecRunner.html");
// "file:///c:/source/SpecRunner.html"
resolvePath("SpecRunner.html");
// "file:///c:/source/JasmineTest/SpecRunner.html"

这里还有一个较长的版本,应该更容易理解,和resolvePath一样;

function longerVersionOfResolvePath (relativePath) {
  var protocol = "file:///c:/";
  var baseUrl = "source/JasmineTest";
  var reUpward = /\.\.\//g;
  var upwardCount = (relativePath.match(reUpward) || []).length;

  var walkUpwards = upwardCount > 0;
  var relativePathWithUpwardStepsRemoved = relativePath.replace(reUpward, "");
  var folderWalkedUpTo = baseUrl.split("/").slice(0, -upwardCount).join("/");

  if (walkUpwards) {
    return protocol + folderWalkedUpTo + "/" + relativePathWithUpwardStepsRemoved;
  }
  else {
    return protocol + baseUrl + "/" + relativePath;
  }
}
于 2012-07-17T10:11:16.810 回答