[英]ES6 import nested function - mocha
我正在使用ES6,并且想开始使用mocha&chai进行测试。
我当前的测试文件代码是:
const assert = require('chai').assert;
var app = require('../../../../src/app/login/loginController').default;
describe('login Controller tests', function(){
it('no idea ', function(){
let result = app();
assert.equal(result, 'hello');
})
})
而我的loginController.js是:
class LoginController {
checkout(){
return 'hello';
}
}
export default LoginController
我想将'checkout'函数导入到测试文件中的变量中,但是到目前为止,我只能导入该类。
将感谢任何帮助,谢谢!
您不能直接从类中导入方法。 如果要将不带类的函数作为中介导入,则需要在类外部定义函数。 或者,如果您真的想将checkout
作为实例方法,则需要在实例上调用它。
这是从您的文件派生的示例文件:
export class LoginController {
// Satic function
static moo() {
return "I'm mooing";
}
// Instance method
checkout() {
return "hello";
}
}
// A standalone function.
export function something() {
return "This is something!";
}
还有一个测试文件,它行使了所有功能,并根据您在问题中显示的文件改编而成:
const assert = require('chai').assert;
// Short of using something to preprocess import statements during
// testing... use destructuring.
const { LoginController, something } = require('./loginController');
describe('login Controller tests', function(){
it('checkout', function(){
// It not make sense to call it without ``new``.
let result = new LoginController();
// You get an instance method from an instance.
assert.equal(result.checkout(), 'hello');
});
it('moo', function(){
// You get the static function from the class.
assert.equal(LoginController.moo(), 'I\'m mooing');
});
it('something', function(){
// Something is exported directly by the module
assert.equal(something(), 'This is something!');
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.