【问题标题】:How to test functions inside angularjs services using jasmine-karma如何使用 jasmine-karma 测试 angularjs 服务中的功能
【发布时间】:2015-03-30 09:30:49
【问题描述】:

我是 karma-jasmine 测试的新手。我编写了一项服务,它使 http get 调用 api 并返回数据对象。

app.service('service_name',['$http',function($http){
    this.getData=function(urlPath){
        var Data = $http.get(path).success(function(data){
                var getData = data;
                console.log("getData success");
                return getData;
            }).error(function(response){
                var getData = response;
                console.log("getData error");
                return getData;
            });

        console.log(Data);
        return Data;
    };
}]);

从控制器我将 url 传递给此服务函数并使用 .then() 回调获取数据;

service_name.getData(url).then(function(data){
    console.log('expected data: ', data);
});

【问题讨论】:

  • 当我在任何浏览器上运行我的应用程序时,所有 ajax 调用都工作正常。这些 ajax 调用是在控制器的“加载”时触发的。但是当我运行我的业力测试配置时,这些控制器被加载但所有 ajax电话被跳过。
  • 您展示的是服务代码,而不是控制器。你想测试什么?如果你想捕捉来自控制器的触发调用,你必须实例化——nathanleclaire.com/blog/2013/12/13/…
  • service_name.getData(url).then(function(data){ console.log('expected data: ', data); });这段代码是写在控制器里面的……这就是我提到控制器的原因
  • 请展示您的测试...或更好的方法,请分叉此代码jsfiddle.net/krzysztof_safjanowski/sDh35 并为其实施您的服务和测试

标签: angularjs karma-jasmine


【解决方案1】:

我是这样做的:

首先根据article设置测试环境

然后在测试规范中,使用下面的测试模板来测试你的服务功能,感谢 Ben Lesh 的出色blog

'use strict';

describe('service_name', function() {
  var service_name,
    httpBackend,
    resource;

  beforeEach(function() {
    module('yourAppName');

    // The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
    // This allows us to inject a service but then attach it to a variable
    // with the same name as the service in order to avoid a name conflict.
    inject(function($injector, _service_name_) {
      service_name = _service_name_;
      httpBackend = $injector.get('$httpBackend');
    });
  });

  // make sure no expectations were missed in your tests.
  // (e.g. expectGET or expectPOST)
  afterEach(function() {
    httpBackend.verifyNoOutstandingExpectation();
    httpBackend.verifyNoOutstandingRequest();
  });

  it('should have a getData function', function() {
    expect(angular.isFunction(service_name.getData)).toBe(true);
  });

  it('getData function should return data object if http request is successful', function() {
    var expectedData = data;
    httpBackend.expectGET('urlPath')
      .respond(expectedData);

    var actualResult;
    service_name.getData().then(function(response) {
      actualResult = response;
    });

    httpBackend.flush();

    expect(actualResult).toEqual(expectedData);
  });

  it('getData should return error message when http request is unsuccessful', function() {
    httpBackend.expectGET('urlPath').respond(500);

    var promise = service_name.getData('urlPath'),
      result = null;

    promise.then(function(foo) {
      result = foo;
    }, function(reason) {
      result = reason;
    });
    httpBackend.flush();
    expect(result).toBe("getData error");
  });

});

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2018-01-31
    • 2014-05-04
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 2013-05-15
    • 1970-01-01
    • 2018-12-23
    相关资源
    最近更新 更多