Below is the snippet of code that I am currently trying to test:
import {inject} from 'aurelia-framework';
import {CrudResource, DependencyFactory} from 'utils';
let commonData = {};
@inject(DependencyFactory.of(CrudResource))
export class CommonDataCache {
constructor(crudResourceFactory) {
this.crudResource = crudResourceFactory('/Common');
}
data() {
return Object.keys(commonData).length > 0 ? commonData :
this.crudResource.get().then(response => {
commonData.clientEntities = response;
return commonData;
});
}
}
The focus now shifts towards the testing aspect (only essential parts have been included for conciseness):
beforeEach(() => {
container = new Container();
container.registerInstance('DependencyFactory.of(CrudResource)', new CrudResourceFactoryMock());
templatingEngine = container.get(TemplatingEngine);
cdc = templatingEngine.createViewModelForUnitTest(CommonDataCache);
});
In essence, my objective is to provide a mocked factory when injecting the resource (the factory enables configuring the injected dependency upon instantiation), in order to pass in a mocked dependency. The hurdle I'm encountering is that despite registering a mocked factory for the resolution of
'DependencyFactory.of(CrudResource)'
, the CommonDataCache class seems to be instantiated with its original dependency instead of the mocked one. It appears that aurelia is not recognizing that a mocked factory has been registered.
Your help and insights are greatly appreciated.