是否可以使用Jasmine测试工具或任何其他npm模块(如rewire或类似模板)测试下面的代码?
const AuthValidatorDumb = require('./src/AuthValidatorDumb');
const AuthValidator = require('./src/AuthValidator');
const config = require('../config');
let instance;
if (!instance) {
if (config.get('auth.enabled')) {
instance = AuthValidator;
} else {
instance = AuthValidatorDumb;
}
}
module.exports = instance;
答案 0 :(得分:0)
我有一个用于测试上述代码的变体。假设你有:
1)上述问题中index.js的代码。
2) AuthValidator.js :
$createdTime = Carbon::parse($specialist->created_at)
->diff(Carbon::now());
$final_string = '';
$year = '';
$month = '';
$days = '';
if ($createdTime->y) {
$year = $createdTime->y . ' yr';
}
if ($createdTime->m) {
$month = ' '. $createdTime->m . ' mo';
}
if ($createdTime->d) {
$days = ' ' . $createdTime->d . ' days';
}
$final_string = trim($year . $month . $days);
3) AuthValidatorDumb.js :
class AuthValidator {}
module.exports = AuthValidator;
这是 test / index.spec.js :
class AuthValidatorDumb {}
module.exports = AuthValidatorDumb;