lagoshny/ngx-hal-client

Unittests

Closed this issue · 2 comments

Hello Lagoshny,

how can i unit testing implement service with your extends RestService in my class?

thanxs for your follow answer

Hi @sebakiller2018,

To testing some service that extends RestService you need import to your spec through TestBed.configureTestingModule the NgxHalClientModule and add service provider {provide: 'ExternalConfigurationService', useClass: ExternalConfigurationService}.

For example you have UserService like this:

user.service.ts:

@Injectable()
export class UserService extends RestService<User> {

    constructor(injector: Injector) {
        super(User, 'users', injector);
    }

    ....

}

Then you will have test like this:

user.service.spec.ts:

describe('UserService', () => {
    let userService: UserService;
    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [NgxHalClientModule], // Add NgxHalClientModule
            providers: [
                UserService,
                // Add ExternalConfigurationService provider it's needed to configure NgxHalClientModule
                {provide: 'ExternalConfigurationService', useClass: ExternalConfigurationService} 
           ]
        });
        userService = TestBed.get(UserService);
    });

    it('should be created', () => {
        expect(userService).toBeDefined();
    });
});

Hi Lagoshny,

thanks a lot for your quick info, it help's a little bit more i have the same idea. My Problem is the mocking of Ressource Type but i think i found a solution. Helpfull for mee where, when you make a example with unittests.

greetings and thanks a lot

sebastian