2016-06-23 65 views
0

我終於找到了TTD!測試是偉大的,但我有一個問題。我正在測試一個可以進行HTTP調用的函數,並收到一個名爲result的大型JSON對象。 result是一個對象數組,它有點看起來像這樣:用TypeScript和Jasmine測試對象數組結構

{ 
    itemsPerPage: 2, 
    numPages:1, 
    offset:0, 
    actualPage:1, 
    firstPostingOnPage:1, 
    lastPostingOnPage:2, 
    totalNumberOfJobs:2, 
    result: [ 
     { 
      jobPostingId:1, 
      postingVersionId:1, 
      title: 'first job', 
      applyOnlineLink: 'some link', 
      config: null, 
      jobStartDate: 1234567890, 
      postingVersionStartDate:1234567890, 
      postingVersionEndDate:1234567890 
     } 
    ] 
}; 

result可具有任何長度,但從來沒有零。我想在結果中測試對象的結構。在我的應用程序,我有以下測試:

it('The object array should have an object that contains keys "jobPostingId, postingVersionId, title" ',() => { 

        let result:Observable<any> = sut.getJobs(); 

        return result.subscribe((content:any) => { 

         assertThat(content, hasProperty('result', 

          containsInAnyOrder({ 
           'jobPostingId': is(number()), 
           'postingVersionId': is(number()), 
           'title': is(string()), 
           'applyOnlineLink': is(string()), 
           'config': is(falsy()), 
           'jobStartDate': is(number()), 
           'postingVersionStartDate': is(number()), 
           'postingVersionEndDate': is(number()) 
          }) 
         )); 
        }); 
       }); 

,因爲我得到的錯誤

Expected: an object with {result: [{"jobPostingId":{},"postingVersionId":{},"title":{},"applyOnlineLink":{},"config":{},"jobStartDate":{},"postingVersionStartDate":{},"postingVersionEndDate":{}}] in any order} 
[2]   but: result no item in [{"jobPostingId":1,"postingVersionId":1,"title":"first job","applyOnlineLink":"some link","config":null,"jobStartDate":1234567890,"postingVersionStartDate":1234567890,"postingVersionEndDate":1234567890}, {"jobPostingId":2,"postingVersionId":1,"title":"second job","applyOnlineLink":"some link","config":null,"jobStartDate":1234567890,"postingVersionStartDate":1234567890,"postingVersionEndDate":1234567890}] matches: {"jobPostingId":{},"postingVersionId":{},"title":{},"applyOnlineLink":{},"config":{},"jobStartDate":{},"postingVersionStartDate":{},"postingVersionEndDate":{}} 

我明明做錯了什麼,爲什麼它期望在對象中的每個屬性後,對象這個失敗來自對象數組。

在此先感謝,對不起,如果我問了一個愚蠢的問題。

回答

1

我是使用了錯誤的匹配,等等......這個工作......

it('The object array should have an object that contains keys "jobPostingId, postingVersionId, title", etc.... ',() => { 

        let result:Observable<any> = sut.getJobs(); 

        return result.subscribe((content:any) => { 

         assertThat(content, hasProperty('result', 
          contains(
           hasProperties({ 
            'jobPostingId': is(number()), 
            'postingVersionId': is(number()), 
            'title': is(string()), 
            'applyOnlineLink': is(string()), 
            'config': is(null), 
            'jobStartDate': is(number()), 
            'postingVersionStartDate': is(number()), 
            'postingVersionEndDate': is(number()) 
           }), 
           hasProperties({ 
            'jobPostingId': is(number()), 
            'postingVersionId': is(number()), 
            'title': is(string()), 
            'applyOnlineLink': is(string()), 
            'config': is(null), 
            'jobStartDate': is(number()), 
            'postingVersionStartDate': is(number()), 
            'postingVersionEndDate': is(number()) 
           }) 
          ) 
         )); 
        }); 
       });