简体   繁体   中英

Provider not found when injecting in Angular unit tests?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM