code

종속성이있는 AngularJS 팩토리 단위 테스트

codestyles 2020. 10. 17. 10:32
반응형

종속성이있는 AngularJS 팩토리 단위 테스트


Angular 팩토리 (Karma + Jasmine 사용)를 단위 테스트 할 때 테스트중인 팩토리에 스텁 종속성을 어떻게 삽입합니까?

내 공장은 다음과 같습니다.

mod = angular.module('myFactoryMod', []);

mod.factory('myFactory', [
  '$log', 'oneOfMyOtherServices', function($log, svc) {
    return makeSomethingThatDoesSomethingWithTheseDependencies($log, svc);
  }
]);

oneOfMyOtherServices 내 공장을 인스턴스화 할 때 필요합니다.

내 테스트는 다음과 같습니다.

it('can get an instance of my factory', function() {
  var oneOfMyOtherServicesStub;

  angular.mock.module('myFactoryMod');

  oneOfMyOtherServicesStub = {
    someVariable: 1
  };

  //****How do I get my stub in my target? ****

  angular.mock.inject(['myFactory', function(target) {

      expect(target).toBeDefined();

    }
  ]);
})

NB 나는 $controller이것이 컨트롤러에 대해 허용 한다는 것을 알고 있지만 공장에 대해서는 동등한 것을 보지 못합니다.


내가 알고있는 이와 같은 작업을 수행하는 방법에는 두 가지가 있습니다.

  1. $provide모의를 주입 하려면 및 익명 모듈을 사용하십시오 .
  2. 조롱하려는 서비스를 삽입하고 재스민의 스파이 기능을 사용하여 모의 가치를 제공합니다.

두 번째 옵션은 테스트중인 코드가 주입 된 서비스에서 호출 할 메서드를 정확히 알고있는 경우에만 작동하며 쉽게 모의 처리 할 수 ​​있습니다. 방법이 아닌 서비스의 데이터 속성에 액세스하는 것처럼 보이므로 첫 번째 옵션을 추구하는 것이 가장 좋습니다.

사용 $provide은 대략 다음과 같습니다.

describe('myFactory', function () {
  // Load your module.
  beforeEach(module('myFactoryMod'));

  // Setup the mock service in an anonymous module.
  beforeEach(module(function ($provide) {
    $provide.value('oneOfMyOtherServicesStub', {
        someVariable: 1
    });
  }));

  it('can get an instance of my factory', inject(function(myFactory) {
    expect(myFactory).toBeDefined();
  }));
});

@bentsai의 의견은 실제로 서비스 테스트에 매우 유용합니다. 완전성을 위해 나는 예를 추가하고 있습니다.

다음은 jasmine당신이 찾고있는 것을 대략적으로 수행 하는 테스트 입니다. 참고 : angular-mocks포함 해야합니다 ( module같은 기능을 제공합니다 inject).

describe('app: myApp', function() {
  beforeEach(module('myApp'));
  var $controller;
  beforeEach(inject(function(_$controller_) {
    $controller = _$controller_;
  }));
  // Factory of interest is called MyFactory
  describe('factory: MyFactory', function() {
    var factory = null;
    beforeEach(inject(function(MyFactory) {
      factory = MyFactory;
    }))
    it('Should define methods', function() {
      expect(factory.beAwesome).toBeDefined()
      expect(factory.beAwesome).toEqual(jasmine.any(Function))
    });
  });
});

이것은 모듈 및 관련 팩토리 정의가 어떻게 생겼는지에 대한 스텁입니다.

var app = angular.module('myApp', []);
app.factory('MyFactory', function() {
  var factory = {};
  factory.beAwesome = function() {
    return 'Awesome!';
  }
  return factory;
});

In this case, it is clear the use of inject() allows you to pull in dependencies, just as you would expect in your normal angular application - and as such you can build up requirements to support testing things which rely on them.

참고URL : https://stackoverflow.com/questions/16565531/unit-testing-angularjs-factories-that-have-dependencies

반응형