Reputation: 1655
I have a method "onEdit" in a component. In that method I've called service method "getAttributeById", I am doing a api call in "getAttributeById" service method and return an observable and I am writing a unit test case for it but I ma getting an error
" Expected spy getAttributeById to have been called."
onEdit(row) {
if (this.role != 'Admin' && this.role != 'Owner') {
return;
}
this.loadingData = true;
this._service.getAttributeById(row['id']).subscribe((data) => {
this.editing = true;
this.newAttribute = { ...data };
data.isEdit = true;
this.loadingData = false;
this._service.setData({attribute: this.newAttribute});
this.router.navigate(['attributes-edit']);
}) }
spec file
describe('MetaDataComponent', () => {
let component: MetaDataComponent;
let fixture: ComponentFixture<MetaDataComponent>;
beforeEach(() => {
const metaDataServiceStub = {
getVariables: query1 => ({ subscribe: () => ({}) }),
getAttributeById: arg1 => ({ subscribe: () => ({}) }),
setData: object1 => ({}),
deleteAttribute: arg1 => ({ subscribe: () => ({}) })
};
const routerStub = { navigate: array1 => ({}) };
TestBed.configureTestingModule({
schemas: [NO_ERRORS_SCHEMA],
declarations: [MetaDataComponent],
providers: [
{ provide: MetaDataService, useValue: metaDataServiceStub },
{ provide: Router, useValue: routerStub }
]
});
spyOn(MetaDataComponent.prototype, 'getAttributes');
fixture = TestBed.createComponent(MetaDataComponent);
component = fixture.componentInstance;
});
it('onEdit', () => {
const payload = {id: '11111'};
const metaDataServiceStub: MetaDataService = fixture.debugElement.injector.get(MetaDataService);
const editSpy = spyOn(metaDataServiceStub, 'getAttributeById').and.returnValue(of({}));
expect(component.onEdit(payload)).toBeUndefined();
expect(editSpy).toHaveBeenCalled();
});
I am getting below error
Expected spy getAttributeById to have been called.
Error: Expected spy getAttributeById to have been called.
at stack (http://localhost:9876/absoluteC:/Users/U1192079/Projects/r2d2-ui/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:2455:17)
at buildExpectationResult (http://localhost:9876/absoluteC:/Users/U1192079/Projects/r2d2-ui/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:2425:14)
at Spec.expectationResultFactory (http://localhost:9876/absoluteC:/Users/U1192079/Projects/r2d2-ui/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:901:18)
at Spec.addExpectationResult (http://localhost:9876/absoluteC:/Users/U1192079/Projects/r2d2-ui/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:524:34)
at Ex
Upvotes: 0
Views: 3166
Reputation: 391
I'm not an expert on this testing framework but I notice the line
const editSpy = spyOn(metaDataServiceStub, 'getAttributeById').and.returnValue(of({}));
I don't know if the part ".and.refurnValue" is part of the matching. If it is, I think for the method getAttributeById to be considered "called" you say it should return an empty object.... Which I think it will never do because you've set it up to return an object that contains a subscribe() method when called.
Upvotes: 1