import { ValidatorsService } from 'services/ValidatorsService.ts';
import { AppConstants } from 'app.constants';
import { AlertsService } from 'services/AlertsService';
import { TestBed } from '@angular/core/testing';
fdescribe('Validators service', function() {
let vs: ValidatorsService = null;
let appConstantsSpy: jasmine.SpyObj<AppConstants>;
beforeEach(() => {
console.log('Nitish');
const spy = jasmine.createSpyObj(
'AppConstants', ['INVALID_NAME_CHARS']);
TestBed.configureTestingModule({
providers: [ValidatorsService,
AlertsService,
{provide: AppConstants, useValue: spy}]
});
vs = TestBed.get(ValidatorsService);
appConstantsSpy = TestBed.get(AppConstants);
appConstantsSpy.INVALID_NAME_CHARS.and.returnValue('#xyz');
});
describe('validators service', () => {
it('should correctly validate entity names', () => {
expect(vs.isValidEntityName('b')).toBe(true);
expect(vs.isValidEntityName('b ')).toBe(true);
expect(vs.isValidEntityName(' b')).toBe(true);
expect(vs.isValidEntityName('bd')).toBe(true);
console.log(AppConstants.INVALID_NAME_CHARS);
expect(vs.isValidEntityName('')).toBe(false);
expect(vs.isValidEntityName(' ')).toBe(false);
expect(vs.isValidEntityName('x')).toBe(false);
expect(vs.isValidEntityName('y')).toBe(false);
expect(vs.isValidEntityName('bx')).toBe(false);
});
这里有问题吗?我无法模拟AppConstants中存在的INVALID_NAME_CHARS属性。它仍然指向原始值(使用测试中编写的console.log检查)
答案 0 :(得分:0)
好的,我知道了。这有两个问题:
appConstantsSpy.INVALID_NAME_CHARS = '#xyz'
。{provide: AppConstants, useValue: spy}
仅在将服务注入到构造函数中且因此间谍没有被嘲笑的情况下才有效。实际上,我本身就是在获得原始价值。同样,console语句将返回原始值。我们需要做console.log(appConstantsSpy.INVALID_NAME_CHARS)
。