2017-12-20 2 views
0

테스트 ID에 '새로운 ServerNotificationApi'를 쓰면 생성자가 호출되지 않습니다. new ServerNotificationApi.constructor()이 작동하지만 왜 쓸 수 없는지 알 수 없습니다. new ServerNotificationApi 유닛 테스트에서 오류가 발생했습니다. 'TypeError : _serverNotifications.default가 생성자가 아닙니다. '왜 생성자가 es6에 필요합니까?

클래스

class ServerNotificationApi { 
     constructor() { 
      SignalR.initConnection(url.serverNotificationHubName) 
     } 

     subscribe = callback => SignalR.subscribe(url.entityChanged, url.serverNotificationHubName, callback); 

     unsubscribe = callback => SignalR.unsubscribe(url.entityChanged, url.serverNotificationHubName, callback); 
    } 

    export default new ServerNotificationApi() 

테스트

it('constructor should call signalR method \'initConnection\'',() => { 
     sinon.stub(SignalR, 'initConnection') 

     new ServerNotificationApi.constructor() 

    SignalR.initConnection.calledWith(url.serverNotificationHubName).should.be.true 

     SignalR.initConnection.restore() 
    }) 

답변

6
export default new ServerNotificationApi() 
       ↑↑↑ 

당신은 수출 클래스 인스턴스이 아니라 클래스 자체. 당신은 본질적으로 다음을 수행하고 있습니다.

let foo = new ServerNotificationApi(); 
new foo(); 

어느 것이 작동하지 않습니다. newexport으로 제거하십시오.

관련 문제