我在 ThingBuilderService 中引入了 MyDataApiService 依赖项,现在 ThingBuilderService 测试失败。如何模拟 MyDataApiService 并在测试中告诉 ThingBuilderService?
export default class ThingBuilderService {
public static $inject = ['MyDataApiService'];
public myData: any[];
/**
* Construct an instance of ThingBuilderService.
*
* @param {xyz.MyDataApiService} myDataApiService The MyDataApiService object.
*/
constructor(myDataApiService: xyz.MyDataApiService) {
myDataApiService.getSomeData()
.then((response) => this.myData = response.data);
}
//...
}
测试:(我已经包含了显示我认为我需要做的部分的代码,但我不明白如何将这些部分组合在一起。
describe('Thing Builder Service', () => {
var service;
var mockMyDataApiService = {};
beforeEach(() => {
var mockMyDataApiService.getSomeData = () => {
var deferred = $q.defer();
deferred.resolve({ data: [
{
'ItemId': 1010101,
'Description': 'asdfasdf'
},
{
'ItemId': 1010102,
'Description': 'jkjkjkjk'
}
]});
return deferred.promise;
};
// *********** now what? ***********
// and do I have to do something to make '$q'
// available in the getSomeData function?
angular.mock.module('abc.module');
angular.mock.inject(_ThingBuilderService_ => {
service = _ThingBuilderService_;
});
});
// tests here
it('should ...', () => { ... });
});
测试运行给出如下错误:
错误:[$injector:unpr] 未知提供者:MyDataApiServiceProvider <- MyDataApiService <- ThingBuilderService
和这个:
错误:未为 MyDataApiService 定义基本 URL