If you have already tried this then maybe post some more details why it didn’t work for you?
Edit:
// Or just a string:
@inject('AppStorage')
export class LocalStorageService {
constructor(storage: Storage) { // storage is built in interface for local storage
this.storage = storage;
}
public set(key: string, value: any) {
this.storage.setItem(key, JSON.stringify(value);
}
}
//
auContainer.registerInstance('AppStorage', mockStorage);
describe("PrintContractService", () => {
let auContainer: Container;
let sut: PrintContractService;
beforeEach(() => {
const mockLocalStorageService = {
get() { return null; },
set() { return null; },
};
auContainer = new Container();
auContainer.registerInstance(HttpClient);
auContainer.registerInstance(LocalStorageService, mockLocalStorageService);
sut = auContainer.get(PrintContractService);
});
}
Is there any documentation that explains this? All the document on this site refers to mocking components (which I haven’t yet got to). Nowhere could I find on Google an explanation of how to test a service and ignore its ctor dependencies, with the exception of registering an HttpClient instance.
I love aurelia, but some things are just so damn difficult.
This is my first project where I have gone beyond the basics. It is fun learning about how to do things differently and more effectively, but some things (such as testing) are just so frustrating.
The issue you described above is how DI works, I’m happy to answer your Q about it, but I’m not sure how to help you get through it in the most appropriate way. I learnt it through trial and errors. Maybe you can start with https://aurelia.io/docs/fundamentals/dependency-injection#introduction, don’t hesitate to come back here with Q, i’m pretty sure folks will be happily helping you out.