2017-01-22 8 views
0

私のイオンアプリのユニットテストはJasmineです。私は、ファイルのアップロードを処理するサービス機能を持っています。スコープがJavaScript内の関数内にある厄介なスコープをテストする

fileUpload(filePath: string,apiEndpoint: string){ 
    const fileTransfer = new Transfer(); 
    let fileName = filePath.substr(filePath.lastIndexOf('/') + 1); 
    let options = Object.assign(this.httpHeader(),{chunkedMode: false, fileName: fileName}); 
    return fileTransfer.upload(filePath, apiEndpoint, options) 
    .then((data) => { 
     return data; 
    }).catch(this.handleError); 
    } 

fileTransferの範囲は、関数内にあるので、テストに使用できません。 fileTransfer.uploadの呼び出しは、cordovaライブラリであるため、関数内で失敗します。

it('updateImage updates the image of user',(done)=>{ 
    auth.fileUpload("path","url").then((data)=>{ 
     done(); 
    }) 
    }) 

考えられる解決策は、this.fileTransfer = new Transfer();です。ライブラリを模倣するか、それを傍受する他の方法はありますか?

答えて

0

私はこれを実行せずにこれを書いていますが、これらの行に沿ったものが必要なものの開始点かもしれません。

describe('updateImage',() => { 
    beforeEach(() => { 
     spyOn(window, 'Transfer').and.returnValue({ 
      upload: jasmine.createSpy('fileTransfer.upload').and.returnValue(
       Promise.resolve({ 
        the: 'data that fileTransfer.upload returns' 
       }) 
      ) 
     }); 
    }); 
    afterEach(() => { 
     window.Transfer.reset(); 
    }); 
    it('updateImage updates the image of user', done => { 
     auth.fileUpload("path", "url").then(data => done()); 
    }); 
}); 
+0

私はこのエラー ''エラーを取得:を:転送()メソッドは 'あなたから' Transfer'を見つけてください ' – raj

+0

存在しないのですか?グローバルではない? –

+0

私は私がテストしている私のサービスでそれを持っています。 'ionic-native'から '' import {Transfer} ''; '' – raj

関連する問題