2017-12-20 28 views
0

なぜテスト用IDで 'new ServerNotificationApi'を書くとコンストラクタが呼び出されないのですが、new ServerNotificationApi.constructor()が動作しますが、なぜ書くのか分かりません。new ServerNotificationApiユニットテストでエラーが発生しました。 '_ Type_Result:_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に置き換えてください。

関連する問題