简体   繁体   English

Angular / Jasmine:在回调中添加.error似乎破坏了单元测试

[英]Angular/Jasmine: adding .error to callback seems to break unit tests

I encountered an error that occurs only when I have the .error part of my http callback defined. 我遇到了仅在定义了HTTP回调的.error部分时发生的错误。 Here is the relevant part of the controller I'm testing: 这是我正在测试的控制器的相关部分:

describe('SimpleControllerTests', function () {

    var scope;
    var expectedResponse = [{count:'101', time:1416960000000}];
    var $httpBackend, $controller, $timeout, uuid4;
    beforeEach(module('dashboardApp'));

    beforeEach(inject(function(_$rootScope_, _$controller_, _$httpBackend_, _$timeout_,_uuid4_){
        $controller = _$controller_;
        $httpBackend = _$httpBackend_;
        $timeout =_$timeout_;
        scope = _$rootScope_;
        uuid4 = _uuid4_;
        spyOn(uuid4,'generate').and.returnValue("1");
    }));

    // makes sure all expected requests are made by the time the test ends
    afterEach(function() {
      $httpBackend.verifyNoOutstandingExpectation(); 
      $httpBackend.verifyNoOutstandingRequest();
    });

    describe('should load data successfully', function() {

        beforeEach(function() {
           $httpBackend.expectPOST('http://localhost/folder/index', {"jsonrpc":"2.0","method":"getData","id":"1","params":{"period":"week"}}).response(expectedResponse); 
            $controller('HomeCtrl as vm', { $scope: scope });
           $httpBackend.flush();
        });

        it('using dirtyTestGraph()', function() {
          scope.vm.dirtyTestGraph(); 
          $timeout.flush();
          scope.$digest();
          expect(scope.vm.chartData).toEqual(expectedResponse);
        });
    });

    describe('should fail to load data', function() {
        beforeEach(function() {
                      $httpBackend.expectPOST('http://localhost/folder/index',
            {"jsonrpc":"2.0","method":"getData","id":"1","params":{"period":"week"}}).response(500);

           $controller('HomeCtrl as vm', { $scope: scope });
           $httpBackend.flush();
        });

        it('using dirtyTestGraph()', function() {
          scope.vm.dirtyTestGraph();
          $timeout.flush();
          scope.$digest();
          expect(scope.vm.chartData).toEqual('');
        });
    });
});

httpBackend.verifyNoOutstandingExpecation fails on // <- Unsatisfied requests: POST http://localhost/folder/index httpBackend.verifyNoOutstandingExpecation失败// //-不满意的请求:POST http:// localhost / folder / index

as does $httpBackend.expectPost in the beforeEach with: 'undefined' is not a function (near '...":"week"}}).response(expectedResponse); 就像beforeEach中的$ httpBackend.expectPost一样:'undefined'不是一个函数(在'...“:” week“}}附近).response(expectedResponse);

it also appears that my scope isn't getting created properly, I get: 'undefined' is not an object evaluating 'scope.vm.graphLoading' 似乎我的作用域没有正确创建,我得到:'undefined'不是评估'scope.vm.graphLoading'的对象

I use the 'controller as' syntax for declaring controllers, and i've used 'ctrl as vm' in other tests and i'm able to reference it fine, not sure what's going on there. 我使用“ controller as”语法声明控制器,并且在其他测试中也使用“ ctrl as vm”,并且我可以很好地引用它,不确定发生了什么。

I forgot the controller method: 我忘记了控制器方法:

vm.dirtyTestGraph = function() {
            $timeout(function(){
                ChartService.get( { period: 'week'} )
                .success(function(res){
                    vm.graphLoading = false;
                    vm.chartData = res.data;
                }).error(function(err){
                    console.log(err);
                });
            }, 2000);
    };

if you look at this: 如果你看这个:

ChartService.get( {...} ).success(function(res){...}).error(function(err){...});

you will see, that you call error on "success", and not on "get", because you chained the functions 您会看到,您将错误称为“成功”,而不是“ get”,因为您已链接函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM