lagoshny / ngx-hal-client

Spring HAL client for Angular
21 stars 15 forks source link

Unittests #1

Closed sebakiller2018 closed 5 years ago

sebakiller2018 commented 5 years ago

Hello Lagoshny,

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

thanxs for your follow answer

lagoshny commented 5 years ago

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();
    });
});
sebakiller2018 commented 5 years ago

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