首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NgModule 'DynamicTestModule‘的无效提供程序-只允许提供程序和类型的实例,got:[.,.,??[ object ]?]

NgModule 'DynamicTestModule‘的无效提供程序-只允许提供程序和类型的实例,got:[.,.,??[ object ]?]
EN

Stack Overflow用户
提问于 2022-05-27 17:49:50
回答 1查看 3.5K关注 0票数 1

在我的角度应用程序中,我重定向到登录页面,如果用户没有登录,它就能工作,但是使用jasmine和因果报应的测试失败了,因为这个错误:Invalid provider for the NgModule 'DynamicTestModule' - only instances of Provider and Type are allowed, got: [..., ...,?[object Object]?]

这是我的规范文件:

代码语言:javascript
运行
复制
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');
    }));

  });

这是我的组成部分:

代码语言:javascript
运行
复制
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"])
    }
    }
    
    )
  }
}

没什么太复杂的,但我不知道如何修复这个测试。

EN

回答 1

Stack Overflow用户

发布于 2022-05-27 21:49:45

通常,RouterTestingModule只进入imports声明。从providers中删除它应该有效。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72409302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档