6

我有一项服务将我的其他几个服务作为依赖项。我如何模拟它进行单元测试?

myApp.factory('serviceToTest',
           ['serviceDependency',
    function(serviceDependency) {
        return function(args) {
            return cond(args) ? serviceDependency() : somethingElse();
        };
    }
]);

在上面的示例中,我想模拟出来,serviceDependency以便验证它是否被调用。我怎样才能做到这一点?

我可以在测试中执行以下操作:

describe("Services", function() {
    describe('serviceToTest', function() {

        myApp.factory('serviceDependency', function() {
            var timesCalled = 0;
            return function() {
                return timesCalled++;
            }
        });

        it('should do foo', inject(function(serviceToTest, serviceDependency) {
            serviceToTest(["foo", "bar", "baz"]);
            expect(serviceDependency()).to.equal(1);
        });
    });
});

这适用于需要模拟的测试,但它会影响随后所有其他测试的状态,这显然是一个问题。

4

3 回答 3

11

如果我理解正确,您想测试一个依赖于另一个服务的服务并模拟每个测试的依赖关系。如果是这样,假设我们car有一个依赖于 a 的 a engine

var app = angular.module('plunker', [])
  .factory('car', function(engine) {
     return {
       drive : function() {
         return 'Driving: ' + engine.speed();
       }
     }
  })
  .value('engine', {
    speed : function() {
      return 'fast';
    }
  });

然后你想测试一辆汽车并模拟一个引擎。有两种方法:或者通过定义一个新模块,我们可以在其中重新定义一个引擎:

describe('Testing a car', function() {
  var testEngine;

  beforeEach(function(){
    testEngine = {};
    angular.module('test', ['plunker']).value('engine', testEngine);
    module('test');
  });   

  it('should drive slow with a slow engine', inject(function(car) {
    testEngine.speed = function() {
      return 'slow';
    };
    expect(car.drive()).toEqual('Driving: slow');
  }));
});

在这里工作的笨蛋:http://plnkr.co/edit/ueXIzk? p =preview

一个更简单的替代方案,基于 JavaScript 的动态特性:

describe('Testing a car', function() {
  var testEngine;

  beforeEach(module('plunker'));
  beforeEach(inject(function(engine){
    testEngine = engine;
  }));

  it('should drive slow with a slow engine', inject(function(car) {
    testEngine.speed = function() {
      return 'slow';
    };
    expect(car.drive()).toEqual('Driving: slow');
  }));
});

http://plnkr.co/edit/tlHnsJ?p=preview

另一种选择是使用 Jasmine 的间谍:

describe('Testing a car', function() {
  var testEngine;

  beforeEach(module('plunker'));
  beforeEach(inject(function(engine){
    testEngine = engine;
  }));

  it('should drive slow with a slow engine', inject(function(car) {
    spyOn(testEngine, 'speed').andReturn('slow');
    expect(car.drive()).toEqual('Driving: slow');
  }));
});

http://plnkr.co/edit/K4jczI?p=preview

于 2012-12-01T21:18:19.883 回答
7

我在尝试将模拟服务注入另一个提供商时遇到了同样的问题。

Pete Bacon Darwin 的这篇文章让我走上了正轨:https ://groups.google.com/d/msg/angular/TvBknXnjRyQ/xtCDkJyqp6MJ

这是一个示例应用程序:

angular.module('MyApplication', [])
    .service('MyServiceDependency', [function(){
        // Behaviour here.
    }])
    .factory('MyFactory', ['MyServiceDependency', function(MyServiceDependency){
        // Behaviour here.
    }]);

我们想模拟MyServiceDependency测试的行为,MyFactory所以我们这样编写测试:

describe(function(){

    var MyFactory;

    beforeEach(function(){
        // 1. Include your application module for testing.
        angular.mock.module('MyApplication');

        // 2. Define a new module.
        // 3. Define a provider with the same name as the one you want to mock (in our case we want to mock 'MyServiceDependency'.
        angular.module('MyAppMock', [])
            .service('MyServiceDependency', function(){
                // Define you mock behaviour here.
            });

        // 4. Include your new mock module - this will override the providers from your original module.
        angular.mock.module('MyAppMock');

        // 5. Get an instance of the provider you want to test.
        inject(function($injector){
            // `MyFactory` will receive the mocked version of `MyServiceDependency`.
            MyFactory = $injector.get('MyFactory');
        });
    });

    it('MyFactory does something special', function(){
        MyFactory(); // Test your provider.
    });

});
于 2014-02-24T07:56:23.150 回答
3

这是我的开源项目中的一个示例:https ://github.com/lucassus/mongo_browser/blob/f1faf1b89a9fc33ef4bc4eced386c30bda029efa/spec/javascripts/app/services_spec.js.coffee#L25 (对不起coffeescript)。通常在规范中,您必须创建并包含一个覆盖给定服务的新模块。

于 2012-11-28T09:29:53.167 回答