Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Angular Unit Testing: Observable not returning results

const MOCK_GET_URL_RESPONSE = 'test';

export class MockService() {
    // Implement service functions here
    getUrl(): Observable<any> {
        return of(MOCK_GET_URL_RESPONSE);
    }
}

describe('ABCComponent', () => {
    let component: ABCComponent;
    let fixture: ComponentFixture<ABCComponent>;
    let service: MockService;

    beforeEach(async () => {
        await TestBed.configureTestingModule({
            declarations: [ABCComponent],
            imports: [CommonModule, HttpClientTestingModule],
            // Provide your service and indicate which
            // class is to be used to mock it
            providers: [{ provide: MyService, useClass: MockService }],
        }).compileComponents();
    });

    it('should create', () => {
        service.getUrl().subscribe((res) => {
            console.log(res); // test
            expect(res).not.toBeNull(); // true
        });
    });
});
 
PREVIOUS NEXT
Tagged: #Angular #Unit #Observable #returning #results
ADD COMMENT
Topic
Name
8+4 =