Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 角度单元测试工厂。catch()_Angularjs_Unit Testing - Fatal编程技术网

Angularjs 角度单元测试工厂。catch()

Angularjs 角度单元测试工厂。catch(),angularjs,unit-testing,Angularjs,Unit Testing,我正在对工厂进行400响应的单元测试,我不明白为什么测试用例中的.catch()响应没有定义。当承诺与.catch()链接时,测试失败,因为响应未定义,但如果使用.then()则测试成功。为什么$q.reject()响应没有传递给.catch()函数?我看到工厂中的.catch()块正在接收$.reject(),但是当在单元测试中返回响应时。catch是未定义的 工厂 测试 描述('resetTempPassword()',函数(){ var结果 beforeEach(function() {

我正在对工厂进行400响应的单元测试,我不明白为什么测试用例中的.catch()响应没有定义。当承诺与.catch()链接时,测试失败,因为响应未定义,但如果使用.then()则测试成功。为什么$q.reject()响应没有传递给.catch()函数?我看到工厂中的.catch()块正在接收$.reject(),但是当在单元测试中返回响应时。catch是未定义的

工厂

测试

描述('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

我不认为catch是标准$http对象的一部分。然后可以有第二个函数参数,在$http调用失败时调用该参数。我认为这就是您应该在这里使用的:

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;
  });
}
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;
  });
}