Angular 为什么我的角度测试会给出不一致的结果?
在我的angular应用程序中,我们有一个“LayoutContainer”类,它只显示我们网站的标题,然后是一个带有静态链接的导航栏 我需要能够根据登录用户的身份动态创建链接——如果他们是管理员,我希望能够显示新链接。我所做的是从“帐户”模块导入减速机 现在,我看到了单元测试的不一致性,当我按照随机顺序运行测试时,某些旧的测试正在崩溃。其他时候,所有68项测试都通过了 以下是某些组件“应创建”类偶尔弹出的错误:Angular 为什么我的角度测试会给出不一致的结果?,angular,karma-jasmine,ngrx,Angular,Karma Jasmine,Ngrx,在我的angular应用程序中,我们有一个“LayoutContainer”类,它只显示我们网站的标题,然后是一个带有静态链接的导航栏 我需要能够根据登录用户的身份动态创建链接——如果他们是管理员,我希望能够显示新链接。我所做的是从“帐户”模块导入减速机 现在,我看到了单元测试的不一致性,当我按照随机顺序运行测试时,某些旧的测试正在崩溃。其他时候,所有68项测试都通过了 以下是某些组件“应创建”类偶尔弹出的错误: Uncaught TypeError: Cannot read property
Uncaught TypeError: Cannot read property 'authentication' of undefined
at :9876/_karma_webpack_/webpack:/src/app/account/reducers/index.ts:42
at :9876/_karma_webpack_/webpack:/node_modules/@ngrx/store/fesm5/store.js:528
at memoized (:9876/_karma_webpack_/webpack:/node_modules/@ngrx/store/fesm5/store.js:484)
at defaultStateFn (:9876/_karma_webpack_/webpack:/node_modules/@ngrx/store/fesm5/store.js:502)
at :9876/_karma_webpack_/webpack:/node_modules/@ngrx/store/fesm5/store.js:531
at memoized (:9876/_karma_webpack_/webpack:/node_modules/@ngrx/store/fesm5/store.js:484)
at :9876/_karma_webpack_/webpack:/node_modules/@ngrx/store/fesm5/store.js:501
at Array.map (<anonymous>)
at defaultStateFn (:9876/_karma_webpack_/webpack:/node_modules/@ngrx/store/fesm5/store.js:501)
at :9876/_karma_webpack_/webpack:/node_modules/@ngrx/store/fesm5/store.js:531
at
疯狂的是,当我重新运行它几次时,通常所有的测试都通过了。问题可能是什么
编辑--失败测试的示例
// Angular Core
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
// Angular Material
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
// ngrx
import { StoreModule, combineReducers } from '@ngrx/store';
// Reducers
import * as fromAccount from '../../../account/reducers';
// Component
import { CancelSubscriptionDialogComponent } from './cancel-subscription-dialog.component';
describe('CancelSubscriptionComponent', () => {
let component: CancelSubscriptionDialogComponent;
let fixture: ComponentFixture<CancelSubscriptionDialogComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
StoreModule.forRoot({
account: combineReducers(fromAccount.reducers)
})
],
providers: [
{ provide: MatDialogRef, useValue: {} },
{
provide: MAT_DIALOG_DATA,
useValue: {}
}
],
declarations: [CancelSubscriptionDialogComponent]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(CancelSubscriptionDialogComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
//角形磁芯
从“@angular/core/testing”导入{async,ComponentFixture,TestBed};
//角材料
从“@angular/material”导入{MatDialogRef,MAT_DIALOG_DATA};
//ngrx
从'@ngrx/store'导入{StoreModule,combinereducer};
//还原剂
从“../../../../account/reducers”导入*作为fromAccount;
//组成部分
从“./cancel subscription dialog.component”导入{CancelSubscriptionDialogComponent};
描述('CancelSubscriptionComponent',()=>{
let组件:CancelSubscriptionDialogComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
进口:[
StoreModule.forRoot({
帐户:组合减速机(来自帐户.减速机)
})
],
供应商:[
{provide:MatDialogRef,useValue:{},
{
提供:MAT_DIALOG_数据,
useValue:{}
}
],
声明:[CancelSubscriptionDialogComponent]
}).compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(CancelSubscriptionDialogComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
});
它('应该创建',()=>{
expect(component.toBeTruthy();
});
});
这取决于您的测试在做什么。他们似乎在某种程度上共享这个国家。你能发布你的一些测试吗?当然,我编辑了这篇文章以包含其中一个测试。这取决于你的测试在做什么。他们似乎在某种程度上共享这个国家。你能发布你的一些测试吗?当然,我编辑了这篇文章以包含其中一个测试。
// Angular Core
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
// Angular Material
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
// ngrx
import { StoreModule, combineReducers } from '@ngrx/store';
// Reducers
import * as fromAccount from '../../../account/reducers';
// Component
import { CancelSubscriptionDialogComponent } from './cancel-subscription-dialog.component';
describe('CancelSubscriptionComponent', () => {
let component: CancelSubscriptionDialogComponent;
let fixture: ComponentFixture<CancelSubscriptionDialogComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
StoreModule.forRoot({
account: combineReducers(fromAccount.reducers)
})
],
providers: [
{ provide: MatDialogRef, useValue: {} },
{
provide: MAT_DIALOG_DATA,
useValue: {}
}
],
declarations: [CancelSubscriptionDialogComponent]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(CancelSubscriptionDialogComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});