NgModule“DynamicTestModule”的提供程序无效 - 仅允许提供程序和类型的实例,得到:[..., ..., ?[object Object]?]

问题描述 投票:0回答:3

在我的角度应用程序中,如果用户未登录,我会重定向到登录页面,它可以工作,但是使用 jasmine 和 karma 的测试失败并出现此错误:

Invalid provider for the NgModule 'DynamicTestModule' - only instances of Provider and Type are allowed, got: [..., ...,?[object Object]?]

这是我的规格文件:

import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
  import { TestBed, waitForAsync } from '@angular/core/testing';

  import { RouterTestingModule } from '@angular/router/testing';

  import { AppComponent } from './app.component';
  import { LoginPage } from './modules/user/pages/login/login.page';

  describe('AppComponent', () => {


    beforeEach(waitForAsync(() => {

      TestBed.configureTestingModule({
        declarations: [AppComponent],
        schemas: [CUSTOM_ELEMENTS_SCHEMA],
        imports:[RouterTestingModule],
        providers:[RouterTestingModule.withRoutes([{path:"users/login",component:LoginPage}])]
      }).compileComponents();
    }));

    it('should create the app', waitForAsync(() => {
      const fixture = TestBed.createComponent(AppComponent);
      const app = fixture.debugElement.componentInstance;
      expect(app).toBeTruthy();
    }));

    it('should have menu labels', waitForAsync(() => {
      const fixture = TestBed.createComponent(AppComponent);
      fixture.detectChanges();
      const app = fixture.nativeElement;
      const menuItems = app.querySelectorAll('ion-label');
      expect(menuItems.length).toEqual(12);
      expect(menuItems[0].textContent).toContain('Inbox');
      expect(menuItems[1].textContent).toContain('Outbox');
    }));

    it('should have urls', waitForAsync(() => {
      const fixture = TestBed.createComponent(AppComponent);
      fixture.detectChanges();
      const app = fixture.nativeElement;
      const menuItems = app.querySelectorAll('ion-item');
      expect(menuItems.length).toEqual(12);
      expect(menuItems[0].getAttribute('ng-reflect-router-link')).toEqual('/folder/Inbox');
      expect(menuItems[1].getAttribute('ng-reflect-router-link')).toEqual('/folder/Outbox');
    }));

  });

这是我的组件:

import { Component, OnInit } from '@angular/core';
import {db} from "./../../functions/src/configs/firebase"
import {initializeApp} from "firebase/app"
import { getAuth, onAuthStateChanged } from "firebase/auth";
import { configs } from './configs/credentials';
import { Router } from '@angular/router';
@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss'],
})
export class AppComponent  implements OnInit{
  public appPages = [
    { title: 'Inbox', url: '/folder/Inbox', icon: 'mail' },
    { title: 'Outbox', url: '/folder/Outbox', icon: 'paper-plane' },
    { title: 'Favorites', url: '/folder/Favorites', icon: 'heart' },
    { title: 'Archived', url: '/folder/Archived', icon: 'archive' },
    { title: 'Trash', url: '/folder/Trash', icon: 'trash' },
    { title: 'Spam', url: '/folder/Spam', icon: 'warning' },
  ];
  public labels = ['Family', 'Friends', 'Notes', 'Work', 'Travel', 'Reminders'];
  constructor(public router:Router) {}
  ngOnInit(): void {
    const app = initializeApp(configs.firebase)
    const auth = getAuth()
    onAuthStateChanged(auth,async (user)=>{
      if( user){
      const token = await user.getIdTokenResult(true)
          console.log("user ok è",user)
          console.log("token.claims",token.claims)
    }else{
      this.router.navigate(["/users/login"])
    }
    }
    
    )
  }
}

没什么太复杂的,但我不明白如何修复测试

angular angular-ui-router karma-jasmine
3个回答
4
投票

通常

RouterTestingModule
仅进入
imports
声明。从
providers
中删除它应该可以。


0
投票

要修复笑话测试中的此错误,您需要像这样导入:

 providers: [
            {
                provide: MAT_DIALOG_DATA,
                useValue: { invoice: { providers: [] } },
            },
           
            {
                provide: FormBuilder,
            },
            {
                provide: HttpClient,
            },
            {
                provide: HttpHandler,
            },
            anotherService,
        ],

0
投票

providers 数组中存在额外的逗号🤦u200d♂️,

© www.soinside.com 2019 - 2024. All rights reserved.