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
Unit testing 如何测试router.navigate和subscribe?角2_Unit Testing_Angular_Routes_Jasmine_Subscribe - Fatal编程技术网

Unit testing 如何测试router.navigate和subscribe?角2

Unit testing 如何测试router.navigate和subscribe?角2,unit-testing,angular,routes,jasmine,subscribe,Unit Testing,Angular,Routes,Jasmine,Subscribe,我是Angular 2单元测试的新手,所以我请求您的帮助 我的注销功能: logOut() { this.authService.logOut().subscribe(() => { this.router.navigate(['login']); }); } 以及我的单元测试: describe('HomeComponent', () => { let component: HomeComponent; let fixture: Compo

我是Angular 2单元测试的新手,所以我请求您的帮助

我的注销功能:

logOut() {
    this.authService.logOut().subscribe(() => {
        this.router.navigate(['login']);
    });
}
以及我的单元测试:

describe('HomeComponent', () => {
  let component: HomeComponent;
  let fixture: ComponentFixture<HomeComponent>;
  let authenticationService: AuthenticationService;
  let mockLogOut = {
    logOut: () => { }
  };

  beforeEach(async(() => {
    TestBed.configureTestingModule({
        imports: [
            FormsModule,
            HttpModule,
            CommonModule,
            ReactiveFormsModule,
            TranslateModule,
            RouterTestingModule.withRoutes([
                { path: 'login', component: LoginComponent }
            ])
        ],
        declarations: [HomeComponent, LoginComponent],
        providers: [
            { provide: AuthenticationService, useValue: mockLogOut },
            TranslateService,
            TRANSLATION_PROVIDERS
        ],
        schemas: [CUSTOM_ELEMENTS_SCHEMA]
    })
        .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(HomeComponent);
    authenticationService = TestBed.get(AuthenticationService);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

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

  describe('Authentication Tests', () => {
    it('should log out the user', inject([AuthenticationService], (mockLogin: AuthenticationService) => {
        fakeAsync(() => {
            spyOn(authenticationService, 'logOut');
            let navigateSpy = spyOn((<any>component).router, 'navigate');
            component.logOut();
            expect(mockLogin.logOut).toHaveBeenCalled();
            expect(navigateSpy).toHaveBeenCalledWith(['/log1n']);
        });
    }));
  });
});
description('HomeComponent',()=>{
let组件:HomeComponent;
let夹具:组件夹具;
let authenticationService:authenticationService;
让mockLogOut={
注销:()=>{}
};
beforeach(异步(()=>{
TestBed.configureTestingModule({
进口:[
FormsModule,
HttpModule,
公共模块,
反应形式模块,
TranslateModule,
RouterTestingModule.withRoutes([
{路径:'login',组件:LoginComponent}
])
],
声明:[HomeComponent,LoginComponent],
供应商:[
{提供:AuthenticationService,useValue:mockLogOut},
翻译服务,
翻译供应商
],
模式:[自定义元素\u模式]
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(HomeComponent);
authenticationService=TestBed.get(authenticationService);
组件=fixture.componentInstance;
fixture.detectChanges();
});
它('应该创建',()=>{
expect(component.toBeTruthy();
});
描述('身份验证测试',()=>{
它('应该注销用户',注入([AuthenticationService],(mockLogin:AuthenticationService)=>{
fakeAsync(()=>{
spyOn(身份验证服务,“注销”);
让navigateSpy=spyOn((component.router,'navigate');
组件。注销();
expect(mockLogin.logOut).tohavebeencall();
期望(navigateSpy).tohavencalledwith(['/log1n']);
});
}));
});
});

我想检查用户是否被重定向到路由/登录,但即使我放置了除/login以外的内容,此测试始终成功。我还想知道如何触发模拟路由器的订阅事件。你找到解决办法了吗?