2017-07-18 15 views
0

私はユニットを400レスポンスでテストしていますが、なぜテストケースの.catch()応答が未定義であるのか分かりません。応答が定義されていないために.catch()でプロミスがチェーンされているときにテストが失敗していますが、.then()が使用されていると成功します。この$ q.reject()応答が.catch()関数に渡されないのはなぜですか?ファクトリの.catch()ブロックが$ .reject()を受け取っているが、ユニットテストで応答が返されたときに.catchは未定義です。角ユニットテスト工場.catch()

工場

function resetTempPassword(data) { 
    return $http.post('/reset_temp_password', data) 
    .then(function(response) { 
    console.log('inside then'); 
    console.log(response); 
    return response; 
    }) 
    .catch(function(response) { 
    console.log('inside catch'); 
    console.log(response); 
    return response; 
    }); 
} 

試験

( 'resetTempPassword()'、関数(){ VaRの結果を記述する。

beforeEach(function() { 
    // init local result 
    result = {}; 
    ... 

it('should return 400 when called with invalid temp password', function() { 
    $httpBackend.expectPOST('/reset_temp_password', RESET_TEMP_INVALID).respond(400, RESET_TEMP_ERROR); 
    $httpBackend.whenPOST(API, RESET_TEMP_INVALID).respond(400, $q.reject(RESET_TEMP_ERROR)); 

    expect(idmAdminFactory.resetTempPassword).not.toHaveBeenCalled(); 
    expect(result).toEqual({}); 

    idmAdminFactory.resetTempPassword(RESET_TEMP_INVALID) 
    .catch(function(response) { 
    result = response; 
    }); 
    // flush pending HTTP requests 
    $httpBackend.flush(); 

    expect(idmAdminFactory.resetTempPassword).toHaveBeenCalledWith(RESET_TEMP_INVALID); 
    expect(result.data.code).toEqual(40008); // result.data is undefined 

答えて

0

私はcatchが標準の$ httpオブジェクトの一部だとは思わない。 $ http呼び出しが失敗したときに呼び出される2番目の関数パラメータを持つことができます。私はあなたがここで使うべきものだと思う:

function resetTempPassword(data) { 
    return $http.post('/reset_temp_password', data) 
    .then(function(response) { 
    console.log('inside then'); 
    console.log(response); 
    return response; 
    }, 
    function(response) { 
    console.log('inside catch'); 
    console.log(response); 
    return response; 
    }); 
}