UI路由器interfers与$ httpbackend单元测试,JS角 [英] UI-router interfers with $httpbackend unit test, angular js

查看:293
本文介绍了UI路由器interfers与$ 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);
  });

我得到的错误是:

The error I get is:

Error: Unexpected request: GET views/appBar.html

意见/ appBar.html是我templateUrl:

views/appBar.html is my templateUrl:

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

所以不知何故UI的路由器让我$ 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 并把它添加到您的测试/模拟称为stateMock文件文件夹,在您的人缘配置该文件,如果它尚未回升。

Take this gist https://gist.github.com/wilsonwc/8358542 and 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路由器interfers与$ httpbackend单元测试,JS角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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