Reputation: 6573
I have a service that receives a class in its constructor. I've mocked the injected service and added it as a provider both in the test module and overridden in the test component, but I'm still getting NullInjectorError: No provider for UserService!
Here's the test - please rest assured that I've imported everything that I need:
describe('DataConsentComponent', () => {
let component: DataConsentComponent;
let fixture: ComponentFixture<DataConsentComponent>;
class UserMock extends User {
constructor () {
super();
}
}
class UserServiceMock {
constructor () {
}
}
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [DataConsentComponent],
providers: [
{ provide: 'UserService', useClass: UserServiceMock },
{ provide: 'User', useClass: UserMock }
]
});
TestBed.overrideComponent(
DataConsentComponent,
{
set: {
providers: [
{ provide: 'UserService', useClass: UserServiceMock },
{ provide: 'User', useClass: UserMock }
]
}
});
fixture = TestBed.createComponent(DataConsentComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}));
it('should create', inject([UserService], () => {
expect(component).toBeTruthy();
}));
});
And the class being tested:
import { Component, OnInit } from '@angular/core';
import { UserService } from '@team/user.service';
import { User } from '@team/user.model';
import { GDPR_IS_ACTIVE } from '../../config/constants';
@Component({
selector: 'app-data-consent',
template: ''
})
export class DataConsentComponent {
public User: User;
constructor(private UserService: UserService){
this.UserService.UserSource$.subscribe(
(User: ETMUser) => {
this.User = User;
});
}
getGDPRIsActive(): boolean {
return GDPR_IS_ACTIVE() || false;
}
getIfUserIsClient() {
return this.UserService.getUserIsClient();
}
getIfUserIsEmployee() {
return this.UserService.getUserIsEmployee();
}
showCandidateGDPRInformation (candidate) {
return true;
}
getNavigateLinkLabel(candidate):any {
return 'View';
}
shouldShowNavigate(candidate) {
return true;
}
isSelectable(candidate) {
return true;
}
}
If there's a better way to get that service in, I'm more than willing to refactor.
Upvotes: 0
Views: 1450
Reputation: 25161
Removing the single quotes around the service names in the providers
array in your unit test should do the trick.
You want the TestBed to provide them as classes/objects, not strings, or tokens.
Upvotes: 2