Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Jasmine测试简单的AngularJS服务_Angularjs_Jasmine_Karma Runner_Angularjs Service - Fatal编程技术网

使用Jasmine测试简单的AngularJS服务

使用Jasmine测试简单的AngularJS服务,angularjs,jasmine,karma-runner,angularjs-service,Angularjs,Jasmine,Karma Runner,Angularjs Service,我有一个简单的服务,我正在尝试进行单元测试。不管我怎么做,要么searchService是未知的提供者,要么服务为null(奇怪的是,这不会导致我的测试失败!!) 有人知道我可能做错了什么吗 angular.module('app').service('searchService', function( $q, _ ) { // _ is lodash var cache = [ { id: "current", name: "Current",

我有一个简单的服务,我正在尝试进行单元测试。不管我怎么做,要么searchService是未知的提供者,要么服务为null(奇怪的是,这不会导致我的测试失败!!)

有人知道我可能做错了什么吗

angular.module('app').service('searchService', function( $q, _ ) { // _ is lodash

  var cache = [
    {
      id: "current",
      name: "Current",
      description: "Search current data"
    },
    {
      id: "historical",
      name: "Historical",
      description: "Search historical data"
    }
  ];

  this.getSearchOptions = function() {
    var deferred = $q.defer();
    deferred.resolve( angular.copy( cache ) );
    return( deferred.promise );
  };

  this.getSearchOptionsByID = function( id ) {
    var deferred = $q.defer();
    var searchOption = _.findWithProperty( cache, "id", id );

    if ( searchOption ) {
      deferred.resolve( angular.copy( searchOption ) );
    } else {
      deferred.reject();
    }
    return( deferred.promise );
   };
  }
);
我正在尝试创建一个单元测试,加载到searchService中,以便检查缓存的值:

describe("Unit: Testing Services", function() {
  describe("Search Service:", function() {
    var service = null;

    beforeEach(function() {
      angular.module('app').service('_');
    });
    // I've also tried the commented out code below
    //beforeEach(inject(function(searchService) {
    //this.service = searchService;
    //}));
    //it('should contain an searchService', invoke(function(service) {

    it('should contain an searchService', function(searchService) {
      expect(searchService).not.to.equal(null);
    });

    it('should contain two search options', function(searchService) {
      expect(searchService.getSearchOptions()).to.equal(2);
    });
  });
});

对于不带参数的服务,以下内容应该适用,也许这可以作为一个起点:

describe("Unit: Testing Services", function() {
    describe("Search Service:", function() {

        beforeEach(function() {
            angular.module('app');
        });

        it('should contain a searchService',
           inject(function(searchService) {
                expect(searchService).not.to.equal(null);
        }));

        it('should contain two search options',
            inject(function(searchService) {
                expect(searchService.getSearchOptions()).to.equal(2);
        }));

   });
});

(您也可以在这里查看:)

它让我朝着正确的方向前进。谢谢我放下了angular.module('app'),只做了module('app')。然后改为注入(已经很晚了,我想我的手指也有自己的想法了)很好听。作为旁注,实际上module('app')是angular.mock.module('app')的快捷方式,它允许您创建真实模块的模拟。