2016-04-29 62 views
0

我正在爲Angular2項目編寫單元測試,並且遇到了像下面這樣測試的問題。期望標題對象創建和append方法已被調用正確的參數。有沒有簡單的方法來做到這一點?茉莉花測試打印稿中對象的實例化

myFunction (){ 

    let headers = new Headers(); 
    headers.append('Content-Type', 'application/x-www-form-urlencoded'); 

} 

我需要完成

describe('myFunction', function() { 

    it('should create headers object', function(){ 

    }); 
    it('should call append in headers object with name and value', function(){ 

    });   

}); 
+1

什麼是與此代碼的問題? –

+0

我不知道如何進行單元測試 –

+0

你想測試什麼? –

回答

1
myFunction (){ 

    let headers = new Headers(); 
    headers.append('Content-Type', 'application/x-www-form-urlencoded'); 
    return headers; 
} 
describe('myFunction', function() { 

    it('should create headers object', function(){ 
     let headers = myFunction(); 
     headers.forEach((values: string[], name: string, headers: Map<string, string[]>) => { 
     // do check with each passed header 
     }); 
    }); 
    it('should call append in headers object with name and value', function(){ 

    });   

});