2017-07-21 9 views
0

私はinit関数のためのユニットテストを書くことをしようとしていると私は私がテストでcollectionReport.init()を呼び出していますエラーを取得しています....init()関数をジャスミンでユニットテストするにはどうすればよいですか?

TypeError: undefined is not an object

これは私がテストしようとしているコードです。 ...

class CollectionsReport { 
    constructor({ editCollectionsId, hasCollections}) { 

    this.editCollectionsId = editCollectionsId; 
    this.hasCollections = hasCollections 
} 

init({ id, name }) { 
    this.id = id; 
    this.name = name; 

    // need to test this 
    if (this.hasCollections) { 
     this.collection = this.collections.find(c => c.staticId === 'CAR-COLLECTION'); 
    } 
} 

そして、これはこれまでのところ

describe('CollectionsReport',() => { 
    const collectionArgs = { 
     editCollectionsId: jasmine.createSpy(), 
     hasCollections: false, 
    }; 

    const collections = [ 
      { 
       id: 1, 
       name: 'foo', 
       staticId: 'CAR-COLLECTIONS', 
      }, 
      { 
       id: 2, 
       name: 'bar', 
       staticId: 'TRUCK-COLLECTIONS', 
      }, 
     ]; 

    let collectionReport; 

    beforeEach(() => { 
     collectionReport = new CollectionsReport(collectionArgs); 
    }); 

    describe('.init()',() => { 
     it('should test hasCollections',() => { 
      collectionReport.init(); 

      //test this.hasCollections here 

     }); 
    }); 
}); 

私のテストである私は、その混乱確信しているので、それを修正し、改善する方法についてコメントしてください。ユニットテストのために新しい人に大変感謝しています。

+0

'CollectionsReport'がオブジェクトを期待していますが、配列 –

+0

おかげで、私を送っていますオブジェクトであるcollectionArgsを送信しています。私は配列を送っているのが分かりますか? –

+0

それはプレーンなJavaスクリプトか角度ですか? – Aravind

答えて

0

ないCollectionsReportクラスの目的ですが、多分これは正しい方向に導いてくれるかわから:

class CollectionsReport { 
    constructor({ editCollectionsId, hasCollections}) { 
    this.editCollectionsId = editCollectionsId 
    this.hasCollections = hasCollections 
    } 

    init({ collections, staticId }) { 
    this.hasCollections = !!collections.find(c => c.staticId === staticId) 
    } 
} 

describe('CollectionsReport',() => { 
    const collectionArgs = { 
    editCollectionsId: jasmine.createSpy(), // Not really using it 
    hasCollections: false 
    } 

    const collections = [ 
    { 
     id: 1, 
     name: 'foo', 
     staticId: 'CAR-COLLECTIONS' 
    }, { 
     id: 2, 
     name: 'bar', 
     staticId: 'TRUCK-COLLECTIONS' 
    } 
    ] 

    describe('.init()',() => { 
    let collectionReport 
    beforeEach(() => { 
     collectionReport = new CollectionsReport(collectionArgs) 
    }) 

    it('should test hasCollections',() => { 
     collectionReport.init({ collections, staticId: 'CAR-COLLECTIONS' }) 

     expect(collectionReport.hasCollections).toBe(true) 
    }) 

    it('should test hasCollections',() => { 
     collectionReport.init({ collections, staticId: 'SOMETHING-ELSE' }) 

     expect(collectionReport.hasCollections).toBe(false) 
    }) 
    }) 
}) 
関連する問題