【发布时间】:2017-10-06 15:30:40
【问题描述】:
我有一项具有以下功能的服务
injectService(serviceToInject: string, methodToInvoke: string){
let service = this.$injector.get(serviceToInject);
service[methodToInvoke]();
}
我想知道如何测试这个?我试过这个:
(function () {
'use strict';
describe.only('ServiceA tests', function () {
let ServiceA;
beforeEach(angular.mock.module('main'));
beforeEach(inject(function (_ ServiceA_, _$injector_) {
ServiceA = _ServiceA_;
$injector = _$injector_;
}));
describe.only('injectServiceAndInvoke', function () {
it('given a string serviceToInject which is a valid service name and a string methodToInvoke which is valid method name without parameters, it should inject the service and call the method', () => {
let serviceName = 'validServiceName';
let methodWithoutParams = 'method';
let injectedService = $injector.get(serviceName);
// sandboxSinon.stub(ButtonService.$injector, 'get').withArgs(serviceName).returns(stubbedService);
let methodToBeCalled = sandboxSinon.stub(injectedService, methodWithoutParams).withArgs(undefined);
sandboxSinon.stub(ServiceA, 'tokenizeState').withArgs(methodWithoutParams).returns([methodWithoutParams, undefined]);
ServiceA.injectServiceAndInvoke(serviceName, methodWithoutParams);
expect(methodToBeCalled.calledOnce).to.equal(true);
});
});
});
})();
我得到了错误(正确)服务“validServiceName”不存在。我也尝试对 $injector.get 进行存根,但我不明白应该返回什么存根以及如何从该服务调用该方法。
【问题讨论】:
-
请提供更多测试代码,以便答案考虑在内。
-
测试更新
-
顺便说一句,这感觉很像服务定位器。为什么需要这样做?您是在测试您的实现还是在测试框架?
-
我想有一种抽象的方式从按钮注入服务,所以我写了这段代码,我想测试一下。
标签: angular unit-testing sinon angularjs-injector sinon-chai