在客户端 Mocha 单元测试期间使用外部 class
Using external class during client side Mocha unit testing
我正在 运行 使用 Mocha 使用以下方法对 javascript class 进行单元测试,首先是测试:
var base = require('../moduleone.js');
describe("some test", function() {
it("description", function() {
var check = base.toBeTested(dummyValue)
//test is here ...
});
});
moduleone.js包含要测试的函数:
function toBeTested(category){
//below I calling an assert function defined in moduletwo
//works fine when running in browser
assert(type(category)=='string','category is string type');
//more code..
return something
module.exports.toBeTested = toBeTested;
moduletwo.js:
function assert(outcome, description) {
//see code.tutsplus.com quick and easy javascript testing with assert
var li = outcome ? 'pass' : 'fail';
if (li == 'fail') {
console.log('FAIL: '+description);
}
else {
console.log('PASS: '+description);
}
}
我遇到的问题是 mocha 对模块二一无所知,当 moduleone 函数调用模块二中的函数时,mocha 抛出 ReferenceError: assert is not defined。我怎样才能 link 我的所有依赖项以便 mocha 可以看到它们?
在您的 moduleone.js
中,确保您正在 require
ing moduletwo.js
将您的 assert
功能纳入 moduleone.js
的范围。否则,您会得到 ReferenceError
,不是因为 mocha
的任何原因,而是因为 moduleone
无法访问 assert
.
// moduletwo.js
function assert(outcome, description) { /* your functionality */ }
module.exports = assert
// moduleone.js
var assert = require('./moduletwo')
function toBeTested(category) { /* your functionality that uses assert */ }
module.exports.toBeTested = toBeTested
现在,关于那个教程。如果您按照它来学习如何 制作 一个简单的断言模块,那很好。但是,如果您正在尝试测试一些执行其他操作的代码,请考虑使用现有的断言库,例如 chai。例如:
// example.test.js
var expect = require('chai').expect
var isValidCategory = require('./is-valid-category')
describe('isValidCategory(category)', function () {
it('validates string categories', function () {
expect(isValidCategory('A String Category')).to.be.true
})
it('does not validate non-string categories', function () {
expect(isValidCategory(['an', 'array', 'somehow'])).to.be.false
})
})
// is-valid-category.js
module.exports = function isValidCategory(category) {
return typeof category === 'string'
}
我正在 运行 使用 Mocha 使用以下方法对 javascript class 进行单元测试,首先是测试:
var base = require('../moduleone.js');
describe("some test", function() {
it("description", function() {
var check = base.toBeTested(dummyValue)
//test is here ...
});
});
moduleone.js包含要测试的函数:
function toBeTested(category){
//below I calling an assert function defined in moduletwo
//works fine when running in browser
assert(type(category)=='string','category is string type');
//more code..
return something
module.exports.toBeTested = toBeTested;
moduletwo.js:
function assert(outcome, description) {
//see code.tutsplus.com quick and easy javascript testing with assert
var li = outcome ? 'pass' : 'fail';
if (li == 'fail') {
console.log('FAIL: '+description);
}
else {
console.log('PASS: '+description);
}
}
我遇到的问题是 mocha 对模块二一无所知,当 moduleone 函数调用模块二中的函数时,mocha 抛出 ReferenceError: assert is not defined。我怎样才能 link 我的所有依赖项以便 mocha 可以看到它们?
在您的 moduleone.js
中,确保您正在 require
ing moduletwo.js
将您的 assert
功能纳入 moduleone.js
的范围。否则,您会得到 ReferenceError
,不是因为 mocha
的任何原因,而是因为 moduleone
无法访问 assert
.
// moduletwo.js
function assert(outcome, description) { /* your functionality */ }
module.exports = assert
// moduleone.js
var assert = require('./moduletwo')
function toBeTested(category) { /* your functionality that uses assert */ }
module.exports.toBeTested = toBeTested
现在,关于那个教程。如果您按照它来学习如何 制作 一个简单的断言模块,那很好。但是,如果您正在尝试测试一些执行其他操作的代码,请考虑使用现有的断言库,例如 chai。例如:
// example.test.js
var expect = require('chai').expect
var isValidCategory = require('./is-valid-category')
describe('isValidCategory(category)', function () {
it('validates string categories', function () {
expect(isValidCategory('A String Category')).to.be.true
})
it('does not validate non-string categories', function () {
expect(isValidCategory(['an', 'array', 'somehow'])).to.be.false
})
})
// is-valid-category.js
module.exports = function isValidCategory(category) {
return typeof category === 'string'
}