Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Jasmine进行测试时,给出了一个失败的:Uncaught(承诺中):TypeError:cannotreadproperty';名字';未定义的_Javascript_Angular_Karma Jasmine_Istanbul - Fatal编程技术网

Javascript 使用Jasmine进行测试时,给出了一个失败的:Uncaught(承诺中):TypeError:cannotreadproperty';名字';未定义的

Javascript 使用Jasmine进行测试时,给出了一个失败的:Uncaught(承诺中):TypeError:cannotreadproperty';名字';未定义的,javascript,angular,karma-jasmine,istanbul,Javascript,Angular,Karma Jasmine,Istanbul,所以我使用茉莉花测试和伊斯坦布尔。我尝试测试一个组件 我有一个只创建组件(实例化)的测试用例,如下所示: describe('DossierPersonalDataComponent', () => { let component: DossierPersonalDataComponent; let fixture: ComponentFixture<DossierPersonalDataComponent>; beforeEach(async(() =>

所以我使用茉莉花测试和伊斯坦布尔。我尝试测试一个组件

我有一个只创建组件(实例化)的测试用例,如下所示:

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

  beforeEach(async(() => {


    TestBed.configureTestingModule({
      imports: [RouterTestingModule, HttpClientTestingModule, DossierModule, BrowserModule],
      declarations: [DossierPersonalDataComponent],
      providers: [
        {
          DossierFileService,
          ErrorProcessor,

          provide: DomSanitizer,
          useValue: {
            sanitize: () => 'safeString',
            bypassSecurityTrustHtml: () => 'safeString'
          }
        }
      ]
    })
      .compileComponents()
      .then(() => {
        fixture = TestBed.createComponent(DossierPersonalDataComponent);
        component = fixture.componentInstance;
      });
  }));


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

constructor(
    private dossierService: DossierService,
    private route: ActivatedRoute,
    private sanitizer: DomSanitizer,
    private dossierFileService: DossierFileService,
    private errorProcessor: ErrorProcessor,
    private dialog: MatDialog
  ) {
    this.dossierItems = this.route.snapshot.data.dossierItems;
    this.editDossierForm = this.formBuilder.group({});
    this.editDossierForm.disable();

    this.dossier = this.route.snapshot.data.dossier;
    this.dossierItems = route.snapshot.data.dossierItems;
    this.profileImagefile = this.route.snapshot.data.profileImage;

    this.editDossierForm = this.formBuilder.group({
      firstName: this.formBuilder.control(this.dossier.firstName, [Validators.required, Validators.maxLength(255)]),
      lastName: this.formBuilder.control(this.dossier.lastName, [Validators.required, Validators.maxLength(255)]),
      mobile: this.formBuilder.control(this.dossier.mobile, [Validators.maxLength(255)]),
      company: this.formBuilder.control(this.dossier.company, [Validators.maxLength(255)]),
      buddy: this.formBuilder.control(this.dossier.buddy, [Validators.maxLength(255)]),
      supervisor: this.formBuilder.control(this.dossier.supervisor, [Validators.maxLength(255)]),
      dateOfBirth: this.formBuilder.control(this.dossier.dateOfBirth)
    });
  }
  ngOnInit(): void {
    this.editDossierForm.disable();
  }
但我仍然在“应创建单元测试”中遇到此错误:

DossierPersonalDataComponent > should create
Failed: Uncaught (in promise): TypeError: Cannot read property 'firstName' of undefined
TypeError: Cannot read property 'firstName' of undefined
    at new DossierPersonalDataComponent (http://localhost:9876/_karma_webpack_/src/app/dossier/components/dossier-profile-data/dossier-personal-data/dossier-personal-data.component.ts:69:4)
    at NodeInjectorFactory.DossierPersonalDataComponent_Factory [as factory] (ng:///DossierPersonalDataComponent/ɵfac.js:5:10)
    at getNodeInjectable (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:5641:1)
    at instantiateRootComponent (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:12566:1)
    at createRootComponent (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:26366:1)
    at ComponentFactory$1.create (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:33908:1)
    at initComponent (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:3225:1)
    at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:365:1)
    at AsyncTestZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:763:1)
    at ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:302:1)
那么我要改变什么呢?我的意思是我必须在测试套件中更正什么?还是单元测试


谢谢。

从导入中删除
RouterTestingModule
,因为您不需要它

您还必须模拟激活的路线,以达到所需的目的

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

  beforeEach(async(() => {


    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule, DossierModule, BrowserModule],
      declarations: [DossierPersonalDataComponent],
      providers: [
          DossierFileService,
          ErrorProcessor,
          {
            provide: ActivatedRoute,
            useValue: {
              snapshot: {
                data: {
                  dossier: {
                    firstName: 'hello',
                    lastName: 'world',
                    mobile: '111-111-1111',
                    company: 'acme',
                    buddy: 'bud',
                    supervisor: 'super',
                    dateOfBirth: '1900-01-01',
                  },
                  dossierItems: [], // mock these to your liking
                  profileImage: '',
                }
              }
            }
          },
        {
          // DossierFileService, These have to be outside of the braces
          // ErrorProcessor,

          provide: DomSanitizer,
          useValue: {
            sanitize: () => 'safeString',
            bypassSecurityTrustHtml: () => 'safeString'
          }
        }
      ]
    })
      .compileComponents()
      .then(() => {
        fixture = TestBed.createComponent(DossierPersonalDataComponent);
        component = fixture.componentInstance;
      });
  }));


  it('should create', () => {
    expect(component).toBeTruthy();
  });
});
description('DossierPersonalDataComponent',()=>{
let组件:档案personaldatacomponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
导入:[HttpClientTestingModule,DossierModule,BrowserModule],
声明:[档案personaldatacomponent],
供应商:[
档案档案服务,
错误处理器,
{
提供:激活的路由,
使用价值:{
快照:{
数据:{
档案:{
名字:“你好”,
姓氏:“世界”,
手机:“111-111-1111”,
公司:“acme”,
巴迪:“巴德”,
主管:“超级”,
出生日期:“1900-01-01”,
},
档案:[],//根据您的喜好模拟这些
配置文件映像:“”,
}
}
}
},
{
//档案文件服务,这些必须在支架外面
//错误处理器,
提供:消毒液,
使用价值:{
清理:()=>“安全字符串”,
bypassSecurityTrustHtml:()=>“安全字符串”
}
}
]
})
.compileComponents()
.然后(()=>{
fixture=TestBed.createComponent(DossierPersonalDataComponent);
组件=fixture.componentInstance;
});
}));
它('应该创建',()=>{
expect(component.toBeTruthy();
});
});

那么我需要改变什么?我不明白为什么考试不及格。嗨,谢谢!!Realy nice,只有一个,必须添加在:}:},{//DossierFileService,这些必须在大括号之外//ErrorProcessor,提供:DomSanitizer,useValue:{sanitize:()=>'safeString',bypassSecurityTrustHtml:()=>'safeString'}