Javascript 如何创建Karma/Jasmine单元测试,以检查依赖于其他模块的角度服务

Javascript 如何创建Karma/Jasmine单元测试,以检查依赖于其他模块的角度服务,javascript,angularjs,unit-testing,jasmine,Javascript,Angularjs,Unit Testing,Jasmine,我读了很多文章,但我仍然不知道如何创建它 有一个模块(“a”)具有功能(“C”)的服务(“B”)。该函数正在其他模块(“D”)中使用另一个函数(“E”)。我想测试函数(“C”)的行为,使用不同于函数(“E”)的答案,[真、假等…] 例如: angular.module('A',[])//或angular.module('A',['D'])) .service('B',function(){ this.C=函数(){ return!D.E(); }; 我用约曼角度生成器构建了这个应用程序 谢谢你假

我读了很多文章,但我仍然不知道如何创建它

有一个模块(“a”)具有功能(“C”)的服务(“B”)。该函数正在其他模块(“D”)中使用另一个函数(“E”)。我想测试函数(“C”)的行为,使用不同于函数(“E”)的答案,[真、假等…]

例如:

angular.module('A',[])//或angular.module('A',['D']))
.service('B',function(){
this.C=函数(){
return!D.E();
};

我用约曼角度生成器构建了这个应用程序


谢谢你

假设所有东西都在一个模块下,比如说A

angular.module('A',[])
  .service('B', function(D) { // Using Dependency Injection we inject service D here
    this.C = function() {
      return !D.E();
    }
    return this;
  })
  .service('D', function() {
    this.E = function() { return false; };
    return this;
  });
单元测试:

describe('Unit Test for the Service B', function() {

  var B, D;  

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

  beforeEach(inject(function(_B_, _D_) {
    B = _B_;
    D = _D_;
  }));

  describe('Functionality of method C', function() {

    it('should negate the returned value of the method E in service D', function() {
      var E = D.E();
      var C = B.C();
      expect(E).toBeFalsy();
      expect(C).toBeTruthy();

    });
  });
});
假设它生活在一个不同的模块中——模块Z

angular.module('A',['Z']) // Now we include the module Z here
      .service('B', function(D) { // and again using Dependency Injection we can inject service D here.
        this.C = function() {
          return !D.E();
        }
        return this;
      });
angular.module('Z', [])
      .service('D', function() {
        this.E = function() { return false; };
        return this;
      });
单元测试:

describe('Unit Test for the Service B', function() {

  var B, D;  

  beforeEach(function() {
    module('A');
    module('Z');
  });

  beforeEach(inject(function(_B_, _D_) {
    B = _B_;
    D = _D_;
  }));

  describe('Functionality of method C', function() {

    it('should negate the returned value of the method E in service D', function() {
      var E = D.E();
      var C = B.C();
      expect(E).toBeFalsy();
      expect(C).toBeTruthy();

    });
  });
});

谢谢,这很有帮助。最后一点我还不知道,那就是我如何操纵伪E的答案,用不同的答案测试C的不同行为。