【发布时间】:2016-02-19 05:39:16
【问题描述】:
所以我想进入 Test Driven Development 并决定在我的项目中使用 Jasmine。
问题是,我无法加载固定装置。
通常提出的两种解决方案是:
- 使用 --allow-file-access-from-files 运行 chrome
- 从本地服务器提供文件
所以我使用了第一个解决方案,但没有结果。
然后我设置我的网络服务器的路由,以便 localhost/fixture/my_fixture 将返回 my_fixture.html 的内容。
所以当我手动访问 localhost/fixture/my_fixture 时,fixture 的内容会显示在屏幕上。但是在我的茉莉花规格文件中,当我使用时:
jasmine.getFixtures().fixturesPath = 'http://localhost/fixture'
loadFixtures('quizz_fixture')
我收到以下错误:
Error: Fixture could not be loaded: http://localhost/fixture/quizz_fixture
(status: error, message: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://localhost/fixture/quizz_fixture?_=1455854875950'.)
当我使用错误中给出的 URL 时,我的浏览器会正确显示夹具的内容。
因此,我不明白这个错误的原因。有人有见解吗?
编辑:
- 网络服务器:Apache
- 浏览器:Chrome
- 操作系统:Windows 7
编辑 2
问题来自 jasmine-jquery,在下面的第 139 行,其中调用了 fail 函数。我无法弄清楚发生了什么,因为应该无法加载的 URL 实际上在我的浏览器中加载得很好:
jasmine.Fixtures.prototype.loadFixtureIntoCache_ = function (relativeUrl) {
var self = this
, url = this.makeFixtureUrl_(relativeUrl)
, htmlText = ''
, request = $.ajax({
async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded
cache: false,
url: url,
dataType: 'html',
success: function (data, status, $xhr) {
htmlText = $xhr.responseText
}
}).fail(function ($xhr, status, err) {
throw new Error('Fixture could not be loaded: ' + url + ' (status: ' + status + ', message: ' + err.message + ')')
})
结果是:
Failed to load 'http://localhost/fixture/quizz_fixture.html?_=1456886216017'
在浏览器中调用时有效。我就是不明白。
谢谢。
【问题讨论】:
标签: javascript jasmine tdd fixture