Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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
Angular 角度-如何获取所选路线的组件实例';您在集成测试中导航到了?_Angular_Unit Testing_Typescript_Karma Runner_Karma Jasmine - Fatal编程技术网

Angular 角度-如何获取所选路线的组件实例';您在集成测试中导航到了?

Angular 角度-如何获取所选路线的组件实例';您在集成测试中导航到了?,angular,unit-testing,typescript,karma-runner,karma-jasmine,Angular,Unit Testing,Typescript,Karma Runner,Karma Jasmine,我第一次写了一个集成测试,我对如何进行有点困惑 基本上,到目前为止,我所做的是设置AppComponent,使用/signin路径,这就是我正在测试的 这是我的测试设置: describe('[INTEGRATION] SigninComponent', () => { let component: AppComponent; let fixture: ComponentFixture<AppComponent>; let location: Location;

我第一次写了一个集成测试,我对如何进行有点困惑

基本上,到目前为止,我所做的是设置
AppComponent
,使用
/signin
路径,这就是我正在测试的

这是我的测试设置:

describe('[INTEGRATION] SigninComponent', () => {

  let component: AppComponent;
  let fixture: ComponentFixture<AppComponent>;
  let location: Location;
  let router: Router;
  let usernameFormControlElement;
  let passwordFormControlElement;
  let submitBtn;

  beforeEach(() => {

    TestBed.configureTestingModule({
      imports: [
        AppModule,
        ModuleImports,
        RouterTestingModule.withRoutes([
          {path: 'signin', component: SigninComponent}
        ])
      ],
      providers: [
        Location
      ],
      schemas: [NO_ERRORS_SCHEMA],
      declarations: [SigninComponent]
    });

    fixture = TestBed.createComponent(AppComponent);
    component = fixture.componentInstance;
    location = TestBed.get(Location);
    router = TestBed.get(Router);

    fixture.detectChanges();
  });

  it('should navigate to dashboard when a token is received', async(() => {

    router.navigate(['signin']).then(() => {

      expect(location.path()).toBe('/signin');

      // Get the elements once we're on the right page
      usernameFormControlElement = fixture.debugElement.query(By.css('.username')).nativeElement;
      passwordFormControlElement = fixture.debugElement.query(By.css('.password')).nativeElement;
      submitBtn = fixture.debugElement.query(By.css('#submit')).nativeElement;

      // How exactly would I get a hold of the SigninComponent`instance here?
      const signinComponent = 
    });
  }));
});
description(“[INTEGRATION]SigninComponent”,()=>{
let组件:AppComponent;
let夹具:组件夹具;
让位置:位置;
让路由器:路由器;
让usernameFormControlElement;
让passwordFormControlElement;
让我们提交;
在每个之前(()=>{
TestBed.configureTestingModule({
进口:[
应用模块,
模块化端口,
RouterTestingModule.withRoutes([
{路径:'signin',组件:SigninComponent}
])
],
供应商:[
位置
],
模式:[无错误模式],
声明:[SigninComponent]
});
fixture=TestBed.createComponent(AppComponent);
组件=fixture.componentInstance;
location=TestBed.get(位置);
路由器=测试台.get(路由器);
fixture.detectChanges();
});
它('收到令牌时应导航到仪表板',异步(()=>{
路由器。导航(['signin'])。然后(()=>{
expect(location.path()).toBe('/sign');
//一旦我们到达正确的页面,就可以获取元素
usernameFormControlElement=fixture.debugElement.query(By.css('.username')).nativeElement;
passwordFormControlElement=fixture.debugElement.query(By.css('.password')).nativeElement;
submitBtn=fixture.debugElement.query(By.css('#submit')).nativeElement;
//我怎样才能在这里找到SigninComponent`实例?
常量符号分量=
});
}));
});
现在,在
const signinComponent=
语句所在的位置,我不确定如何继续。我需要在这里获得
SigninComponent
实例,以便访问被动表单模型,为
username
password
表单元素设置一些值,然后提交表单

我不认为我应该调用
TestBed.createComponent(SigninComponent)
然后从中获取
componentInstance
,因为我们已经使用
AppComponent
完成了这项工作

正确的做法是什么

编辑:

我想我可以使用
ActivatedRoute
,但尝试读取
。组件
返回空值