"Jasmine Angular Mock"组件属性 [英] Jasmine Angular Mock component attribute
问题描述
运行命令:
ng test --codeCoverage = true --progress = false --watch = false
ng test --codeCoverage=true --progress=false --watch=false
错误消息:
TypeError:无法读取未定义的属性"substring"
TypeError: Cannot read property 'substring' of undefined
组件的NgOnInit
NgOnInit of the component
private serv: ExtratosMensaisService,
ngOnInit(): void {
const serventia: Serventia = this.serv.getServentiaSelecionada();
const competencia: Competencia = this.serv.getCompetenciaSelecionada();
const titularidade = serventia.titularidade.substring(0, 1).toUpperCase();
}
服务文件方法:
getServentiaSelecionada(): Serventia {
return JSON.parse(sessionStorage.getItem('serventia'));
}
我知道该属性是未定义的,但是我无法使其变为已定义". 我已经尝试过使用jasmine.createSpyObj()中的第三个数组来监视属性,但错误仍然存在. 并尝试在mockExtratosMensaisService returnValue(of({object}))上传递JSON对象,并尝试执行以下操作:
I understand that the attribute is undefined, but I can't make it 'defined'. I already tried to use the third array from the jasmine.createSpyObj() to spy on properties but the error continues. And tried to pass an JSON object on the mockExtratosMensaisService returnValue(of({ object })), and tried this:
mockExtratosMensaisService.getServentiaSelecionada.and.returnValue(of({ titularidade: 123 }));
我使用了错误的方法吗?我需要此测试通过.
Am I using the wrong approach? I need this test to pass.
spec.component文件:
spec.component file:
describe('PrestacaoContasTitularComponent', () => {
let component: PrestacaoContasTitularComponent;
let fixture: ComponentFixture<PrestacaoContasTitularComponent>;
const mockExtratosMensaisService = jasmine.createSpyObj('Obj',
['getServentiaSelecionada', 'getCompetenciaSelecionada'],
['titularidade']);
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ RouterTestingModule, HttpClientTestingModule ],
declarations: [ PrestacaoContasTitularComponent ],
providers: [ MatDialog, Overlay, MatSnackBar,
{ provide: ActivatedRoute, useValue: {} },
{ provide: InjectionToken, useValue: {} },
{ provide: MAT_DIALOG_SCROLL_STRATEGY, useValue: {} },
{ provide: ExtratosMensaisService, useValue: mockExtratosMensaisService }
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(PrestacaoContasTitularComponent);
component = fixture.componentInstance;
mockExtratosMensaisService.getServentiaSelecionada.and.returnValue(of({ titularidade: 123 }));
mockExtratosMensaisService.getCompetenciaSelecionada.and.returnValue(of({ data: 'competencia' }));
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
推荐答案
尝试将模拟更改为:
const mockExtratosMensaisService = jasmine.createSpyObj('Obj', ['getServentiaSelecionada', 'getCompetenciaSelecionada']);
然后做
mockExtratosMensaisService.getServentiaSelecionada.and.returnValue({ titularidade: 'abc' });.
我将123更改为"abc",因为我不确定子字符串是否适用于数字.以前,您仍在返回,这是不正确的,因为它不是可观察的,它是常规的JavaScript对象.
I changed 123 to 'abc' because I am not sure if substring would work on a number. Before, you were still returning of and this is not correct because it is not an observable, it is a regular JavaScript object.
这篇关于"Jasmine Angular Mock"组件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!