javascript - 如何监视服务以测试它 -- AngularJS/Jasmine

标签 javascript angularjs unit-testing jasmine code-coverage

尝试了我在互联网上找到的所有方法来使这项工作没有成功。试图在我的服务中测试一个功能,但根据我的报道,我从未访问过它。任何帮助将不胜感激:)

服务:

'use strict';

angular.module('Service').service('configService', function(
  $rootScope, $http) {
  var configObj = null;
  return {

    getConfig: function() {
      if (configObj != null) {
        console.log("returning cached config");
        return configObj;
      }
      else {
        return $http.get('conf.json').then(function(res) {
          $http.get(res.confLocation).then(function(
            locationResponse) {
            configObj = locationResponse;
            $rootScope.configObj = configObj;
            console.log($rootScope.configObj);
            return configObj;
          });
        });
      }
    }
  };
});

getConfig 在我尝试过的测试中从未被访问过。

服务测试:

'use strict';
describe('Service: configService', function() {

  // load the controller's module
  beforeEach(module('Service'));

  var configService, $httpBackend, results, tstLocation, tstRes;
  var tstConfig = {
    "confLocation": "local-dev-conf.json"
  };

  var tstConfigEmpty = {};
  var tstConfigObjEmpty = {};

  var tstConfigObj = {
    "AWS": {
      "region": "us-east-1",
      "endpoint": "http://localhost:8133"
    }
  };

  // Initialize the controller and a mock scope
  beforeEach(inject(function(_configService_, _$httpBackend_) {
    inject(function($rootScope) {
      $rootScope.USERNAME = 'TESTER';
      $rootScope.configObj = tstConfigObj;
      $rootScope.locationResponse = tstLocation;
      $rootScope.res = tstRes;
    });

    configService = _configService_;
    $httpBackend = _$httpBackend_;

    //Problem here??
    spyOn(configService, 'getConfig').and.callFake(function() {
      return {
        then: function() {
          return "something";
        }
      };
    });

  }));
  it('should return a promise', function() {
    expect(configService.getConfig().then).toBeDefined();
  });

  it('should test backend stuff', inject(function() {

    results = configService.getConfig(tstConfig);
    $httpBackend.expectGET('conf.json').respond(tstConfig);
    $httpBackend.expectGET('local-dev-conf.json').respond(tstConfigObj);
    $httpBackend.flush();
  }));

  //Thanks Miles
  it('should check if it was called', inject(function() {
    results = configService.getConfig().then();
    expect(configService.getConfig).toHaveBeenCalled();

    });
    // console.log(results);
  }));

  it('should check for a null configObj', inject(function() {
    results = configService.getConfig(tstConfigObjEmpty).then(function() {
      expect(results).toBe(null);
    });
    // console.log(results);
    // console.log(tstConfigObj);
  }));

  it('should check for a non-null configObj', inject(function() {
    results = configService.getConfig(tstConfigObj).then(function() {

      // Any string is accepted right now -- Why??
      expect(results).toEqual("returning cached config");
      expect(results).toBe("returning cached config");
      expect(results).toBe("your mom"); // SHOULDN'T BE WORKING BUT DOES
      expect(results).toEqual("Object{AWS: Object{region: 'us-east-1', endpoint: 'http://localhost:8133'}}");
      expect(results).toBe("Object{AWS: Object{region: 'us-east-1', endpoint: 'http://localhost:8133'}}");
    });
    // console.log(results);
    // console.log(tstConfigObj);
  }));

  it('should check for null file', inject(function() {
    results = configService.getConfig(tstConfigEmpty).then(function() {
      expect(results).toEqual(null);
      expect(results).toBe(null);
    });
  }));

  it('should test a valid file', inject(function() {
    results = configService.getConfig(tstConfig).then(function() {
      expect(results).not.toBe(null);
      expect(results).toEqual("Object{confLocation: 'local-dev-conf.json'}");
    })
});

我认为我使用 spyOn 有误,或者在我的测试中没有正确访问 getConfig。想法?

编辑:Here is my code coverage

编辑 2:由于 Miles 发现的问题更改了测试 3,但测试覆盖率仍然没有更新。正如艾米指出的那样,我的 spyOn 逻辑有问题。看来我不应该使用 callFake?

编辑 3:感谢 Miles,现在可以访问该功能了。 必须将我的 spyOn 更改为:

spyOn(configService, 'getConfig').and.callThrough(); 

然后添加测试用例:

results = configService.getConfig(tstConfig).then();   
expect(configService.getConfig).toHaveBeenCalled();

Coverage now (still needs work)

最佳答案

您调用的是假函数而不是函数。因此函数内部的逻辑不会被调用。

关于javascript - 如何监视服务以测试它 -- AngularJS/Jasmine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37885020/

相关文章:

javascript - audio.play() html 标签无法在 chrome 中播放

java - 测试组件类

asp.net-mvc - 如何在 MSTest 中对 JsonResult 和集合进行单元测试

javascript - HTML 页面和脚本加载后,AJAX 就绪事件

javascript - 奇怪的定位元素

javascript - jQuery:cropit 删除图像数据并替换为新数据?

javascript - Angular.js - Google 自动完成事件触发时清除文本框?

AngularJS + Jersey RESTful 后端 : Authentication & Authorization

javascript - Mocha - 测试 Promise, `done` 永远不会在 Promise 中被调用

javascript - 将大量数据从 Web 服务器传输到客户端