Unit Testing Angular Observables

前端 未结 2 487
情歌与酒
情歌与酒 2021-01-22 16:05

I am new to testing world and I have just started writing unit tests for an existing Angular 2 code. I have a function confirmDelete which returns Obserable&l

2条回答
  •  感情败类
    2021-01-22 16:27

    I've built off of peeskillet's answer. The p-confirmDialog component calls the subscribe of the service, so I had to added requireConfirmationSource, which I found in the PrimeNg source. My mock is as follows:

    //
    import { Subject } from 'rxjs/Subject';
    import { Observable } from 'rxjs/Observable';
    //
    export class ConfirmationServiceMock {
        public key: string = 'mock1';
        public header: string = 'Delete Confirmation';
        public icon: string = '';
        public message: string = '';
        //  Call Accept to emulate a user accepting
        public accept: Function;
        //  Call Reject to emulate a user rejecting
        public reject: Function;
        private requireConfirmationSource = new Subject();
        requireConfirmation$ = this.requireConfirmationSource.asObservable();
        //
        public confirm(config: any) {
            console.log( 'In confirm service mock...' );
            this.message = config.message;
            this.accept = config.accept;
            this.reject = config.reject;
            console.log( this.message );
            return this;
        }
    }
    //
    

提交回复
热议问题