如何使用模拟$ httpBackend测试错误分支 [英] How to use mock $httpBackend to test error branch

查看:112
本文介绍了如何使用模拟$ httpBackend测试错误分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面从这里的 AngularJS文档

问题是,该文档描述只有成功/幸福的code的分支,不存在如何测试失败分支的例子。

我想要做的,就是设置precondition用于触发 $ scope.status =错误! code

下面是一个最小的例子。

  //控制器
功能myController的($范围,$ HTTP){  this.saveMessage =功能(消息){
    $ scope.status ='保存...;
    $ http.post('/ add-msg.py,消息).success(功能(响应){
      $ scope.status ='';
    })错误(函数(){
      $ scope.status ='错误';
    });
  };
}//测试控制器
变量$ httpBackend;beforeEach(注入(函数($喷油器){
  $ httpBackend = $ injector.get('$ httpBackend');
}));它('应该送味精到服务器',函数(){  $ httpBackend.expectPOST('/ add-msg.py,短信内容)响应(500,'')。  无功控制范围=美元的新(myController的)。
  $ httpBackend.flush();
  controller.saveMessage('邮件内容');
  $ httpBackend.flush();  //这里的问题是:如何设置$ httpBackend.expectPOST触发
  //这个条件。
  期待(scope.status).toBe(错误!');
});});


您正在检查控制器的特性当您设定的范围的属性。

如果你想在你的期望呼叫测试 controller.status ,你应该设置 this.status 控制器,而不是 $ scope.status 中。

在另一方面,如果设置 $ scope.status 在你的控制器,那么你应该使用 scope.status ,而不是 controller.status 期望电话。


更新:我创建了一个工作版本为大家讲解Plunker:

<一个href=\"http://plnkr.co/edit/aaQ7JQV9WlXhou0PYHTn?p=$p$pview\">http://plnkr.co/edit/aaQ7JQV9WlXhou0PYHTn?p=$p$pview

所有的测试现在路过...

I am following the AngularJS documentation from here

The problem is that the documentation describes only the "success/happy" branch of the code, and there is no example of how to test the "failure" branch.

What I want to do, is to set the precondition for triggering the $scope.status = 'ERROR!' code.

Here is a minimal example.

// controller
function MyController($scope, $http) {

  this.saveMessage = function(message) {
    $scope.status = 'Saving...';
    $http.post('/add-msg.py', message).success(function(response) {
      $scope.status = '';
    }).error(function() {
      $scope.status = 'ERROR!';
    });
  };
}

// testing controller
var $httpBackend;

beforeEach(inject(function($injector) {
  $httpBackend = $injector.get('$httpBackend');
}));

it('should send msg to server', function() {

  $httpBackend.expectPOST('/add-msg.py', 'message content').respond(500, '');

  var controller = scope.$new(MyController);
  $httpBackend.flush();
  controller.saveMessage('message content');
  $httpBackend.flush();

  // Here is the question: How to set $httpBackend.expectPOST to trigger
  // this condition.
  expect(scope.status).toBe('ERROR!');
});

});

解决方案

You are checking a property of the controller while you are setting a property of the scope.

If you want to test for controller.status in your expect call, you should set this.status inside your controller instead of $scope.status.

On the other hand, if you set $scope.status in your controller, then you should use scope.status instead of controller.status in your expect call.


UPDATE: I created a working version for you on Plunker:

http://plnkr.co/edit/aaQ7JQV9WlXhou0PYHTn?p=preview

All tests are passing now...

这篇关于如何使用模拟$ httpBackend测试错误分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆