I am trying to mock currentOrganizationMessageSource from my service but getting getting following error:
this.OrganizationService.currentOrganizationMessageSource.subscribe is not a function
I tried to use spyOnProperty and than I get currentOrganizationMessageSource is not a property
Class:
export class OrganizationEditComponent implements OnInit {
feedback: any = {};
appOrgs: OrganizationDataModel;
organizationLocationTableTitle: string;
constructor(
public route: ActivatedRoute,
private router: Router,
private orgService: OrganizationService) {
}
ngOnInit() {
this.loadOrganisation();
}
private loadOrganisation()
{
this.orgService.currentOrganizationMessageSource.subscribe(
org => this.appOrgs = org);
this
.route
.params
.pipe(
map(p => p.id),
switchMap(id => {
if (id === 'new') { return of(this.appOrgs); }
if (id != null) {
return this.orgService.findByOrganizationsId(id);
}
})
)
.subscribe(orgs => {
this.orgService.changeMessage(orgs);
this.appOrgs = orgs;
this.feedback = {};
},
err => {
this.feedback = {type: 'warning', message:'Error'};
}
);
}
Service:
export class OrganizationService {
private OrganizationMessageSource = new BehaviorSubject( new OrganizationDataModel());
currentOrganizationMessageSource = this.OrganizationMessageSource.asObservable();
changeMessage(appOrganization: appOrganizationDataModel) {
this.appOrganizationMessageSource.next(appOrganization);
}
}
Test spec class:
fdescribe('OrganizationEditComponent', () => {
let component: OrganizationEditComponent;
let fixture: ComponentFixture<OrganizationEditComponent>;
let activatedRoutes: any = {};
beforeEach(async(async () => {
const OrganizationService: OrganizationService = jasmine.createSpyObj('OrganizationService', ['currentOrganizationMessageSource']);
await TestBed.configureTestingModule({
declarations: [ OrganizationEditComponent ],
providers: [
{ provide: ActivatedRoute, useValue: activatedRoutes },
{ provide: Router, useValue: {url: ''}},
{ provide: OrganizationService, useValue: OrganizationService },
],
imports: [ FormsModule ] ,
schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA]
})
.compileComponents();
// component.ngOnInit();
fixture = TestBed.createComponent(OrganizationEditComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}));
function givenServicesAreMocked() {
const spy = TestBed.get(OrganizationService);
// let mockService = jasmine.createSpyObj(OrganizationService, ['currentOrganizationMessageSource']);
// mockService.currentOrganizationMessageSource.and.returnValue('');
// spyOnProperty(OrganizationService, "OrganizationMessageSource").and.returnValue([]);
// OrganizationService.currentOrganizationMessageSource = () => {
// return [];
// };
}
});