简体   繁体   English

单元测试错误 - 失败:Angular 中模块“DynamicTestModule”声明的意外值“[object Object]”

[英]Unit test error - Failed: Unexpected value '[object Object]' declared by the module 'DynamicTestModule' In Angular

I have searched the other questions on stack to try and get a solution for this issue, but none of them either seem to make sense or work.我在堆栈上搜索了其他问题,试图为这个问题找到解决方案,但它们似乎都没有意义或工作。 I am getting the error: Failed: Unexpected value '[object Object]' declared by the module 'DynamicTestModule' I have tried adding the RouterTestingModule.withRoutes in my imports, as that seems to be the solution but that has not fixed the issue, My code so far is:我收到错误: Failed: Unexpected value '[object Object]' declared by the module 'DynamicTestModule'我尝试在我的导入中添加RouterTestingModule.withRoutes ,因为这似乎是解决方案,但并没有解决问题,到目前为止我的代码是:

homeview.component.spec.ts homeview.component.spec.ts

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ContentComponent } from '../../components/content-area/content/content.component';
import { HeaderComponent } from '../../components/header/header/header.component';
import { FooterComponent } from '../../components/footer/footer/footer.component';
import { NewsComponent } from '../../components/news/news/news.component';
import { faHeadphones} from '@fortawesome/free-solid-svg-icons';
import { RouterTestingModule } from '@angular/router/testing';
import { PlaylistViewComponent } from '../../views/playlist-view/playlist-view/playlist-view.component';

import { HomeViewComponent } from './home-view.component';

describe('HomeViewComponent', () => {
  let component: HomeViewComponent;
  let fixture: ComponentFixture<HomeViewComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ HomeViewComponent, HeaderComponent, FooterComponent, ContentComponent, NewsComponent, faHeadphones, PlaylistViewComponent],
      imports: [
        RouterTestingModule.withRoutes(
          [{path: 'home', component: HomeViewComponent}, {path: 'playlist', component: PlaylistViewComponent}]
        )
      ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(HomeViewComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

homeview.ts homeview.ts

import { Component, OnInit } from '@angular/core';
import { ApiService } from '../../services/api.service';

@Component({
  selector: 'app-home-view',
  templateUrl: './home-view.component.html',
  styleUrls: ['./home-view.component.scss']
})
export class HomeViewComponent implements OnInit {

  public playlist = [];

  constructor(private service: ApiService,
    ) { }

  ngOnInit() {
  }

}

app.routing module app.routing 模块

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, Routes } from '@angular/router';
import { HomeViewComponent } from './views/home-view/home-view.component';
import { PlaylistViewComponent } from './views/playlist-view/playlist-view/playlist-view.component';


const appRoutes: Routes = [
  { path: '', redirectTo: '/home' , pathMatch:'full'},
  { path: 'home', component: HomeViewComponent},
  { path: 'playlist', component: PlaylistViewComponent},
];

@NgModule({
  imports: [RouterModule.forRoot(appRoutes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

app.module.ts app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { HeaderComponent } from './components/header/header/header.component';
import { FooterComponent } from './components/footer/footer/footer.component';
import { ContentComponent } from './components/content-area/content/content.component';
import { HomeViewComponent } from './views/home-view/home-view.component';
import { AppRoutingModule } from './app-routing.module';
import { HttpClientModule, HttpClientXsrfModule } from '@angular/common/http';
import { FormsModule } from '@angular/forms';
import { NgxPaginationModule } from 'ngx-pagination';
import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
import { Ng2SearchPipeModule } from 'ng2-search-filter';
import { PlaylistViewComponent } from './views/playlist-view/playlist-view/playlist-view.component';
import { NewsComponent } from './components/news/news/news.component';
import { CarouselModule } from 'ngx-owl-carousel-o';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { RouterModule } from '@angular/router';

@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,
    FooterComponent,
    ContentComponent,
    HomeViewComponent,
    PlaylistViewComponent,
    NewsComponent,
    ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    HttpClientXsrfModule,
    FormsModule,
    NgxPaginationModule,
    FontAwesomeModule,
    Ng2SearchPipeModule,
    CarouselModule,
    BrowserAnimationsModule,
    RouterModule,
    ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

homeView.html主页视图.html

<app-header></app-header>
<app-content></app-content>
<app-news></app-news>
<app-footer></app-footer>

Any idea's?有任何想法吗?

You are trying to test the HomeComponent , so you will only need the imports and declarations related to the HomeComponent , you don't have to import all the application.您正在尝试测试HomeComponent ,因此您只需要与HomeComponent相关的导入和声明,您不必导入所有应用程序。

For the nested component, we can mock them like this: https://angular.io/guide/testing#stubbing-unneeded-components对于嵌套组件,我们可以像这样模拟它们: https : //angular.io/guide/testing#stubbing-unneeded-components

In conclusion, your HomeComponent Spec File should be:总之,您的HomeComponent规范文件应该是:

import { HomeViewComponent } from './home-view.component';
import { ApiService } from '../../services/api.service';

describe('HomeViewComponent', () => {
  let component: HomeViewComponent;
  let fixture: ComponentFixture<HomeViewComponent>;

   @Component({selector: 'app-header', template: ''})
   class AppHeaderStubComponent {}

   @Component({selector: 'app-content', template: ''})
   class AppContenStubComponent { }

   @Component({selector: 'app-news', template: ''})
   class AppNewsStubComponent {}

   @Component({selector: 'app-footer', template: ''})
   class AppFooterStubComponent {}

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ HomeViewComponent,
                 AppHeaderStubComponent,
                 AppContenStubComponent,
                 AppNewsStubComponent,
                 AppFooterStubComponent
      ],
      providers: [ApiService]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(HomeViewComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:模块'DynamicTestModule'声明的意外值'FormGroup' - Error: Unexpected value 'FormGroup' declared by the module 'DynamicTestModule' 失败:模块“DynamicTestModule”导入的意外值“TreeviewConfig”。 请在单元测试中添加@NgModule 注解 - Failed: Unexpected value 'TreeviewConfig' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation in unit test Angular 4 单元测试错误 - 无法在“XMLHttpRequest”上执行“发送”:无法加载“ng:///DynamicTestModule/LoginComponent.ngfactory.js” - Angular 4 Unit test error - Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'ng:///DynamicTestModule/LoginComponent.ngfactory.js' Angular 2模块“ AppModule”声明的异常值“ MyDirective” - Angular 2 Unexpected value 'MyDirective' declared by the module 'AppModule' 错误:由模块“DynamicTestModule”导入的意外值“DomSanitizer”。 请添加@NgModule 注释 - Error: Unexpected value 'DomSanitizer' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation 在Karma Angular Unit Test期间&#39;错误:意外请求&#39; - 'Error: Unexpected request' during Karma Angular Unit Test 创建对象的单元测试 - Unit test for creation object 模块“ AppModule”声明的异常值“ Component” - Unexpected value 'Component' declared by the module 'AppModule' 在 moch 单元测试文件中重置 module.exports 对象 - Reset module.exports object in moch unit test file 模块“ AppModule”声明了意外的模块“ PublicModule”。 Angular4组件结构 - Unexpected module 'PublicModule' declared by the module 'AppModule'. Angular4 Component Structure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM