In our angular app, we use environment files to load some config.
environment.ts
export const environment = {
production: false,
defaultLocale: 'en_US',
};
We then use it in one of our service:
import { environment } from '../../environments/environment';
import { TranslateService } from './translate.service';
@Injectable()
export class LocaleService {
constructor(private translateService: TranslateService){}
useDefaultLocaleAsLang(): void {
const defaultLocale = environment.defaultLocale;
this.translateService.setUsedLang(defaultLocale);
}
}
So I use the values in environment file in a service method.
In our test file, we can of course Spy on the translateService:
translateService = jasmine.createSpyObj('translateService', ['setUsedLang']);
But I don't know how to mock the environment values in my testing file (in a beforeEach
for example). Or even transform it, for testing purpose, to a Subject
so I can change it and test different behaviors.
More generally speaking, how can you mock such imports values in tests to be sure not to use real values?
EnvironmentLoader
) and mock that? – Aged