jeudi 26 octobre 2017

Angular2 Test (jasmine) mocking Service. Can't resolve all parameters for Service: (?, ?)

i am trying to mock following service to test my component:

Production:

@Injectable()
export class UserService extends DtoService {
    // some not relevant stuff.
}

@Injectable()
export abstract class DtoService {
    constructor(private http: Http, private authHttp: AuthHttp) {}

    get() {
        return this.http.get(...);
    }
}

@Component({
    selector: 'users',
    templateUrl: 'users.component.html',
    styleUrls: ['users.scss'],
    providers: [UserService]
})
export class UsersComponent implements OnInit, OnDestroy {
    constructor(private userService: UserService) {}
    // etc
}

Test:

class MockUserService {
    public get(): Observable<User> {
        return Observable.of(new User({id: 1, email: 'user1@test.com'}));
    }
}

beforeEach(async(() => {

    TestBed
        .configureTestingModule({
            declarations: [UsersComponent],
            providers: [{provide: UserService, useClass: MockUserService}]
        });
    this.fixture = TestBed.createComponent(UsersComponent);

}));

It is supposed to be the most classical use-case, right (see angular test doc)? But here I get the Error:

Can't resolve all parameters for UserService: (?, ?).

But UserService is not supposed to be instantiated at all. What am I doing wrong? Thank you community!

Aucun commentaire:

Enregistrer un commentaire