In my situation, there exists a static method that generates an object with service-like functionality, which can be accessed through another static method. Here is an example:
expect class FooService {
private static instance : FooService;
static getInstance() {
if (!this.instance) {
this.instance = new FooService();
}
return this.instance
private constructor() {}
[implementation]
}
The issue at hand is determining whether the `getInstance()` method is being called within my Angular application or during a unit test. This distinction would enable me to return either a mock version of the object or the real one.
This is what I envision:
expect class FooService {
private static instance : FooService;
static getInstance() {
if (isInJasmineTest()} { <----
return new MockFooService()
}
if (!this.instance) {
this.instance = new FooService();
}
return this.instance
}
private constructor() {}
[implementation]
}
While I acknowledge that there are various techniques involving spys, mocks, and injection to accomplish this task,
I am particularly interested in understanding how to incorporate isInJasmineTest()
?
One approach might involve designing a function that gets invoked in the `beforeAll` section of each Jasmine test, such as `setJasmineTestEnvironment(true)`. However, I am curious if there is already an existing mechanism available for inspection without necessitating the creation of an additional function like this.