UI-router interfers with $httpbackend unit test, angular js

后端 未结 5 631
悲&欢浪女
悲&欢浪女 2020-11-29 01:59

This is a controller with a submit function:

$scope.submit = function(){   

 $http.post(\'/api/project\', $scope.project)
      .success(function(data, stat         


        
5条回答
  •  鱼传尺愫
    2020-11-29 02:16

    This Github issue about Unit Testing UI Router explains more fully what's happening.

    The problem is that $httpBackend.flush() triggers a broadcast which then triggers the otherwise case of the stateProvider.

    A simple solution can be to do the following setup, as mentionned by @darinclark in Github thread mentionned above. This is valid if you do not need to test state transitions. Otherwise have a look to @rosswil's answer that is inspired by @Vratislav answer on Github.

    beforeEach(module(function ($urlRouterProvider) {
        $urlRouterProvider.otherwise(function(){return false;});
    }));
    

    EDITED

    Thanks to Chris T to report this in the comments, seems after v0.2.14? the best way to do this is to use

    beforeEach(module(function($urlRouterProvider) {
      $urlRouterProvider.deferIntercept();
    }));
    

提交回复
热议问题