Reputation: 1683
I have a class with config variables
import config from '@/libs/config';
export class Logger {
constructor () {
this.level = this.testMethod(config.LOGGER);
this.target_name = this.testMethod(config.LOGGER_TARGETS);
}
And for example I have a method, where I use config variables
testMethod(config) {
return config;
}
Is it possible to mock up the config variables
(like config.LOGGER or config.LOGGER_TARGETS) to test my testMethod
?
How could I access and set it?
Upvotes: 1
Views: 3526
Reputation: 800
Actually, you can test testMethod
right now.
The approach varies based on what you want to test.
If you want to test that this method is called during creating a new instance, just mock it and check if it was called with required arguments. If you want to test that loggerInstance.level
and loggerInstance.target_name
has correct values, just check them afterward.
If you want to test method's logic – call it with params and check returned value.
Also, you can implement dependency inversion and get Logger class to receive config variables via constructor params. Pass params from @/libs/config
in code that use this class and pass test params in tests. It's much better from the perspective of scalability and architecture.
Upvotes: 1