<small id='0j0sH'></small><noframes id='0j0sH'>

<i id='0j0sH'><tr id='0j0sH'><dt id='0j0sH'><q id='0j0sH'><span id='0j0sH'><b id='0j0sH'><form id='0j0sH'><ins id='0j0sH'></ins><ul id='0j0sH'></ul><sub id='0j0sH'></sub></form><legend id='0j0sH'></legend><bdo id='0j0sH'><pre id='0j0sH'><center id='0j0sH'></center></pre></bdo></b><th id='0j0sH'></th></span></q></dt></tr></i><div id='0j0sH'><tfoot id='0j0sH'></tfoot><dl id='0j0sH'><fieldset id='0j0sH'></fieldset></dl></div>
  • <legend id='0j0sH'><style id='0j0sH'><dir id='0j0sH'><q id='0j0sH'></q></dir></style></legend>

  • <tfoot id='0j0sH'></tfoot>

        <bdo id='0j0sH'></bdo><ul id='0j0sH'></ul>

        UI-router 干扰 $httpbackend 单元测试,角度 js

        时间:2023-09-30
          <tbody id='ghiRn'></tbody>

            • <small id='ghiRn'></small><noframes id='ghiRn'>

            • <i id='ghiRn'><tr id='ghiRn'><dt id='ghiRn'><q id='ghiRn'><span id='ghiRn'><b id='ghiRn'><form id='ghiRn'><ins id='ghiRn'></ins><ul id='ghiRn'></ul><sub id='ghiRn'></sub></form><legend id='ghiRn'></legend><bdo id='ghiRn'><pre id='ghiRn'><center id='ghiRn'></center></pre></bdo></b><th id='ghiRn'></th></span></q></dt></tr></i><div id='ghiRn'><tfoot id='ghiRn'></tfoot><dl id='ghiRn'><fieldset id='ghiRn'></fieldset></dl></div>

                • <bdo id='ghiRn'></bdo><ul id='ghiRn'></ul>
                  <tfoot id='ghiRn'></tfoot>
                  <legend id='ghiRn'><style id='ghiRn'><dir id='ghiRn'><q id='ghiRn'></q></dir></style></legend>

                  本文介绍了UI-router 干扰 $httpbackend 单元测试,角度 js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这是一个带有提交功能的控制器:

                  This is a controller with a submit function:

                  $scope.submit = function(){   
                  
                   $http.post('/api/project', $scope.project)
                        .success(function(data, status){
                          $modalInstance.dismiss(true);
                        })
                        .error(function(data){
                          console.log(data);
                        })
                    }
                  }
                  

                  这是我的测试

                  it('should make a post to /api/project on submit and close the modal on success', function() {
                      scope.submit();
                  
                      $httpBackend.expectPOST('/api/project').respond(200, 'test');
                  
                      $httpBackend.flush();
                  
                      expect(modalInstance.dismiss).toHaveBeenCalledWith(true);
                    });
                  

                  我得到的错误是:

                  Error: Unexpected request: GET views/appBar.html
                  

                  views/appBar.html 是我的 templateUrl:

                  views/appBar.html is my templateUrl:

                   .state('project', {
                      url: '/',
                      templateUrl:'views/appBar.html',
                      controller: 'ProjectsCtrl'
                    })
                  

                  所以不知何故 ui-router 让我的 $httpBackend 指向这个而不是我的提交函数.我在所有使用 $httpBackend 的测试中都遇到了同样的问题.

                  So somehow ui-router is making my $httpBackend point to this instead of my submit function. I have the same issue in all my tests using $httpBackend.

                  有什么解决办法吗?

                  推荐答案

                  抓住这个要点https://gist.github.com/wilsonwc/8358542

                  angular.module('stateMock',[]);
                  angular.module('stateMock').service("$state", function($q){
                      this.expectedTransitions = [];
                      this.transitionTo = function(stateName){
                          if(this.expectedTransitions.length > 0){
                              var expectedState = this.expectedTransitions.shift();
                              if(expectedState !== stateName){
                                  throw Error("Expected transition to state: " + expectedState + " but transitioned to " + stateName );
                              }
                          }else{
                              throw Error("No more transitions were expected! Tried to transition to "+ stateName );
                          }
                          console.log("Mock transition to: " + stateName);
                          var deferred = $q.defer();
                          var promise = deferred.promise;
                          deferred.resolve();
                          return promise;
                      }
                      this.go = this.transitionTo;
                      this.expectTransitionTo = function(stateName){
                          this.expectedTransitions.push(stateName);
                      }
                  
                      this.ensureAllTransitionsHappened = function(){
                          if(this.expectedTransitions.length > 0){
                              throw Error("Not all transitions happened!");
                          }
                      }
                  });
                  

                  将其添加到 test/mock 文件夹中名为 stateMock 的文件中,如果尚未选择该文件,请将该文件包含在您的 karma 配置中.

                  Add it to a file called stateMock in your test/mock folder, include that file in your karma config if it isn't already picked up.

                  测试前的设置应如下所示:

                  The setup before your test should then look something like this:

                  beforeEach(module('stateMock'));
                  
                  // Initialize the controller and a mock scope
                  beforeEach(inject(function ($state //other vars as needed) {
                      state = $state;
                      //initialize other stuff
                  }
                  

                  那么在你的测试中你应该添加

                  Then in your test you should add

                  state.expectTransitionTo('project');
                  

                  这篇关于UI-router 干扰 $httpbackend 单元测试,角度 js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 ui-router 和 ion-tabs 时模板不更新 下一篇:如何实现“ui-sref"?有条件地执行?

                  相关文章

                • <small id='lXGmM'></small><noframes id='lXGmM'>

                      <bdo id='lXGmM'></bdo><ul id='lXGmM'></ul>
                    1. <tfoot id='lXGmM'></tfoot>

                    2. <legend id='lXGmM'><style id='lXGmM'><dir id='lXGmM'><q id='lXGmM'></q></dir></style></legend>
                    3. <i id='lXGmM'><tr id='lXGmM'><dt id='lXGmM'><q id='lXGmM'><span id='lXGmM'><b id='lXGmM'><form id='lXGmM'><ins id='lXGmM'></ins><ul id='lXGmM'></ul><sub id='lXGmM'></sub></form><legend id='lXGmM'></legend><bdo id='lXGmM'><pre id='lXGmM'><center id='lXGmM'></center></pre></bdo></b><th id='lXGmM'></th></span></q></dt></tr></i><div id='lXGmM'><tfoot id='lXGmM'></tfoot><dl id='lXGmM'><fieldset id='lXGmM'></fieldset></dl></div>