angularjs - 如何在没有 Jasmine 和 Angular 模拟的情况下测试 $http?

标签 angularjs jasmine angularjs-http

我想通过对我的服务器的真实调用进行集成测试,所以,我不想使用 angular-mocks 中的 $httpBackend 模块,所以我试试这个:

beforeEach(inject(function($rootScope,_MembersDataSvc_){
    service = _MembersDataSvc_;
}));

it('test',function(done){
    service.me().then(function(){done();});
});

服务是:
function me() {
      return $http
        .get('urlBase/me')
        .then(meSuccess);

        function meSuccess(response) {
            return response.data.members[0];
        }
    }

这从不调用 $http,似乎 angular-mocks 覆盖了 $http 服务并且从未调用过。

一些想法?

编辑 1:

根据这篇文章:http://base2.io/2013/10/29/conditionally-mock-http-backend/

你可以为你不想模拟的 $http 调用做一个 passThrough,所以你试试这个:
var service;
    var scope;
    var $httpBackend;

    beforeEach(inject(function($rootScope,_MembersDataSvc_,_$httpBackend_){
        service = _MembersDataSvc_;
        scope = $rootScope.$new();
        $httpBackend = _$httpBackend_;
    }));

it('test',function(done){
        //this.timeout(10000);
        $httpBackend.whenGET(/views\/\w+.*/).passThrough();
        $httpBackend.whenGET(/^\w+.*/).passThrough();
        $httpBackend.whenPOST(/^\w+.*/).passThrough();
        service.me().then(function(response){console.log(response);done();});
        scope.$apply();
        //service.getDevices(member).then(function(response){console.log(response);done();})
    });

但是 passThrough 在这里是未定义的。

编辑 2:

我读了这篇文章:http://blog.xebia.com/2014/03/08/angularjs-e2e-testing-using-ngmocke2e/ ,但我想那是一个独立的测试??,我想用业力和 Jasmine 来跑。

这是我的全部测试。
describe('integration test', function () {

    beforeEach(function () {
        module('MyAngularApp');
    });

    var service;
    var scope;
    var $httpBackend;

    beforeEach(inject(function($rootScope,_MembersDataSvc_,_$httpBackend_){
        service = _MembersDataSvc_;
        scope = $rootScope.$new();
        $httpBackend = _$httpBackend_;
    }));

    it('test for test',function(done){
        $httpBackend.whenGET(/views\/\w+.*/).passThrough();
        $httpBackend.whenGET(/^\w+.*/).passThrough();
        $httpBackend.whenPOST(/^\w+.*/).passThrough();
        service.me().then(function(response){console.log(response);done();});
        scope.$apply();
    });
});

最佳答案

我推荐使用 ngMidwayTester,它允许您连接到真正的后端,我用它在代码级别进行集成测试 - 所以介于单元测试和 e2e 测试之间:Two types of tests in AngularJS (plus one more) - Full-Spectrum Testing with AngularJS and Karma

关于angularjs - 如何在没有 Jasmine 和 Angular 模拟的情况下测试 $http?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30174542/

相关文章:

c# - 如何在 WebAPI 中发布带有附加文件的对象?

javascript - 如何使用 angularjs 指令创建可重用组件?

javascript - 如何解决MomentJS和Asp.net Core之间的时差

javascript - Knockout JS 单元测试和 ViewModel 耦合和依赖关系

ruby-on-rails-3 - 使用 Jasmine 和 Rails 3.1 测试 Coffeescript

javascript - 如何在 Jasmine 中显示通过测试?

javascript - 添加文档访问被拒绝

angularjs - Angular : $http requests gives -1 status

angularjs - ng-click $event 在 Mozilla Firefox 中不起作用

javascript - SyntaxError : Unexpected token o at Object. 解析( native )AngularJS