問題描述
我有一個簡單的 JavaScript 文件 color.js
和一個匹配的規范文件 colorSpec.js
.
I have a simple JavaScript file, color.js
, and a matching spec file, colorSpec.js
.
color.js:
function Color()
{
}
colorSpec.js:
require('./color.js');
describe("color", function() {
it("should work", function() {
new Color(255, 255, 255);
});
});
當我運行 jasmine-node colorSpec.js
時,我得到以下異常:
When I run jasmine-node colorSpec.js
, I get the following exception:
ReferenceError: Color is not defined
如何讓 Jasmine 在運行 colorSpec.js
之前加載我的 color.js
文件?
How can I get Jasmine to load my color.js
file before running colorSpec.js
?
推薦答案
您可以使用 require() 在 colorSpec.js 中加載您的 color.js.如果你不告訴茉莉花它們在你的規范文件中到底是什么,我看不出茉莉花是如何猜測所有依賴關系的.編輯 :一個快速而骯臟的解決方案,但也許有一些內置的 Jasmine 可以做到這一點:
you could load your color.js in the colorSpec.js with a require(). I dont see how jasmine can guess all the dependencies without you telling jasmine what they are exactly in your spec file. Edit : A quick and dirty solution , but maybe there is something builtin Jasmine to do that :
fs = require('fs')
myCode = fs.readFileSync('./color.js','utf-8') // depends on the file encoding
eval(myCode)
那么你的課程應該可以與 jasmine 一起使用
then your class should be available with jasmine
如果你直接在你的文件上調用 require 我認為你需要創建一個模塊并導出它
if you call require directly on your file i think you need to create a module and export it
這篇關于如何使用 Jasmine 節點加載文件進行測試?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!