Нет поставщика услуг при проведении углового теста с использованием кармы и жасмина - PullRequest
0 голосов
/ 24 сентября 2019

Я пытаюсь запустить угловые тесты, используя in karma и jasmine, и получаю следующую ошибку в одном из моих компонентов.

NullInjectorError: StaticInjectorError(DynamicTestModule)[AuthService -> MonitoringService]: 
  StaticInjectorError(Platform: core)[AuthService -> MonitoringService]: 
    NullInjectorError: No provider for MonitoringService!

Компонент внедрил AuthService, а AuthService внедрил MonitoringService.Как добавить MonitoringService в качестве поставщика в файл спецификации моего компонента

Вот спецификация моего компонента.Если вы видите, я добавил MonitoringService в разделе провайдеров в его beforeEach.Но я все еще получаю вышеуказанную ошибку

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { FormsModule } from '@angular/forms';
import { TranslateModule, TranslateLoader } from "@ngx-translate/core";
import { HttpClientModule } from '@angular/common/http';

import { AuthService } from '../../../services/auth.service';
import { AlertService, MessageSeverity, DialogType } from '../../../services/alert.service';
import { ConfigurationService } from '../../../services/configuration.service';
import { LocalStoreManager } from '../../../services/local-store-manager.service';
import { AppTitleService } from '../../../services/app-title.service';
import { AppTranslationService, TranslateLanguageLoader } from '../../../services/app-translation.service';
import { NotificationService } from '../../../services/notification.service';
import { NotificationEndpoint } from '../../../services/notification-endpoint.service';
import { EndpointFactory } from '../../../services/endpoint-factory.service';
import { UserEditorModalComponent } from '../user/user-editor-modal.component';
import { UserDeleteModalComponent } from '../user/user-delete-modal.component';
import { ChangePasswordComponent } from './change-password.component';
import { AccountService } from '../../../services/account.service';
import { AccountEndpoint } from '../../../services/account-endpoint.service';
import { User } from '../../../models/security/user.model';
import { MessageViewerComponent } from  '../../shared/message-viewer/message-viewer.component';
import { MonitoringService } from '../../../services/monitoring/monitoring.service';

describe('ChangePasswordComponent', () => {
    let component: ChangePasswordComponent;
    let authService: AuthService;
    let accountService: AccountService;
    let alertService: AlertService;

    beforeEach(async(() => {
      TestBed.configureTestingModule({
        imports: [
            HttpClientModule,
            RouterTestingModule,
            FormsModule,
            TranslateModule.forRoot({
                loader: {
                    provide: TranslateLoader,
                    useClass: TranslateLanguageLoader
                }
            })
        ],
        declarations: [ MessageViewerComponent, ChangePasswordComponent ],
        providers: [
            MonitoringService,
            AuthService,
            AlertService,
            ConfigurationService,
            LocalStoreManager,
            AppTitleService,
            AppTranslationService,
            NotificationService,
            NotificationEndpoint,
            EndpointFactory,
            AccountService,
            AccountEndpoint
        ]
      })
      .compileComponents();
    }));

    beforeEach(() => {

      authService = TestBed.get(AuthService);
      accountService = TestBed.get(AccountService);
      alertService = TestBed.get(AlertService);

      const currentUser = new User('42', 'Chuck Norris');
      component = new ChangePasswordComponent(null, accountService, alertService, authService);

      spyOn(alertService, 'showMessage').and.callThrough();
    });

Служба мониторинга

import { map } from 'rxjs/operators';
import { MonitoringEndpoint } from './monitoring-endpoint.service';
import { TradeNotification } from '../../models/trade/trade-notification.model';

@Injectable()
export class MonitoringService {

    constructor(private router: Router, private monitoringEndpoint: MonitoringEndpoint) {

    }

    NotifyTradeStarted(authUserId: number): any {
        return this.monitoringEndpoint.NotifyTradeStartedEndpoint<number>(authUserId);
    }

    CheckExecuteTrade(model: TradeNotification): Promise<any> {
        return this.monitoringEndpoint.CheckExecuteTrade(model);
    }

    logoutNotification(identityUserName: string, refreshToken: string): any {
        return this.monitoringEndpoint.logoutNotificationEndpoint<string>(identityUserName, refreshToken);
    }

    RefreshClientDetails(): any {
        return this.monitoringEndpoint.refreshClientDetailsEndpoint();
    }
}
...