У меня есть свойство только для чтения в одном из сервисов в angular:
@Injectable({
providedIn: 'root'
})
export class MyService {
private readonly timeIntervals: any;
}
constructor(private readonly config: AppConfig) {
this.timeIntervals = this.config.getResourceByKey(this.timeIntervalString);
}
getTimeIntervalMenuItem(): Array<IElementData>{
const menuListItems = new Array<IElementData>();
let index = 0;
for (const item of Object.keys(this.timeIntervals)) {
menuListItems.push({
id: this.menuItem + index.toString(),
label: this.timeIntervals[item].toString()
});
index++;
}
return menuListItems;
}
export interface IElementData {
label?: string;
value?: string;
id?: string;
active?: string;
}
Во время написания теста для того же сервиса, как я буду присваивать значение timeIntervals?
Что Я сделал следующее:
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
MyService,
]
});
service = TestBed.get(MyService);
});
it('should generate total 7 menu items for dropdown', () => {
const timeIntervals = 'timeIntervals';
service[timeIntervals] = timeLists;
const generatedMenu = service.getTimeIntervalMenuItem();
expect(generatedMenu.length).toBe(7);
});
const timeLists = {
3: '3',
5: '5',
10: '10',
15: '15',
20: '20',
30: '30',
60: '60'
};
Я получаю сообщение об ошибке:
Невозможно назначить значение timeIntervals, поскольку оно доступно только для чтения.
Как присвоить значение timeIntervals, чтобы я мог проверить свой метод