lundi 13 juillet 2020

Jasmine Angular Mock component attribute

Running command:

ng test --codeCoverage=true --progress=false --watch=false

Error message:

TypeError: Cannot read property 'substring' of undefined

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();
}

The service file method:

getServentiaSelecionada(): Serventia {
   return JSON.parse(sessionStorage.getItem('serventia'));
}

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 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();
});

});

Aucun commentaire:

Enregistrer un commentaire