[英]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.