2017-11-26 16 views
0

저는 ES6을 사용하고 있으며 mocha & chai를 사용하여 테스트를 시작하고 싶습니다.ES6 가져 오기 중첩 기능 - mocha

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 

내가 내 테스트 파일 내부의 변수에 '체크 아웃'기능을 가져올하지만, 지금까지 수업을 가져올 수있었습니다.

도움을 주셔서 감사합니다.

+0

방금 ​​생성하지 마십시오 : 당신은 당신의 질문에 표시 파일에서 적응

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!"; } 

그리고 모든 기능을 행사하는 테스트 파일 : 여기

는 예를 들어, 당신에서 파생 된 파일입니다 테스트의 새로운 LoginController() 인스턴스가 그 인스턴스에서 함수를 호출합니까? – Andy

+0

그래,하지만 인스턴스가 필요 없어, 난 단지 함수가 필요해. 가져온 함수를 변수로 가질 수 있습니까? –

+0

가능한 [Javascript의 정적 함수 선언과 일반 함수 선언의 차이점] (https://stackoverflow.com/questions/45594196/difference-between-static-function-declaration-and-the-normal-function- declarati) – str

답변

0

클래스에서 직접 메서드를 가져올 수 없습니다. 중개자가 아닌 클래스가없는 함수를 가져 오려면 클래스 외부에서 함수를 정의해야합니다. 또는 실제로 checkout을 인스턴스 메소드로 지정했다면 인스턴스에서 호출해야합니다.

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!'); 
    }); 
});