首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用karma-Jasmine测试进行页面导航(路由)

如何使用karma-Jasmine测试进行页面导航(路由)
EN

Stack Overflow用户
提问于 2019-09-27 15:22:50
回答 1查看 566关注 0票数 0

我正在编写angular (Karma-Jasmine)测试用例,我想在页面之间导航。如何使用karma-Jasmine在页面之间导航。

EN

回答 1

Stack Overflow用户

发布于 2019-09-27 18:18:14

1)测试使用导航的组件:执行操作时应调用导航方法(如toHaveBeenCalled或toHaveBeenCalledWith之类的断言)

代码语言:javascript
复制
it('should redirect the user to the users page after saving', () => {
  let router = TestBed.get(Router);
  let spy = spyOn(router, 'navigate');

  component.save();

  expect(spy).toHaveBeenCalledWith(['users'])
});

2)还要测试您的路由是否将使用正确的组件

代码语言:javascript
复制
app.routes.spec.ts
import { routes } from './app.routes'

it('should contain a route for users', () => {
  expect(routes).toContain({path: 'users', component: UserComponent})
});

3)您可以使用useValue来测试不同的activatedRouteParams (只需配置然后传递给providers,参见示例)。组件ts文件示例:

代码语言:javascript
复制
ngOnInit() {
  this.contextType = this.route.snapshot.paramMap.get('contextType');
  this.contextId = this.route.snapshot.paramMap.get('contextId');
  this.contextFriendlyId = this.route.snapshot.paramMap.get('contextFriendlyId');
}

规范文件(configureTestData是一个允许您传递不同可配置值的函数,在我的例子中是activatedRouteParams)

代码语言:javascript
复制
export function configureTestData(activatedRouteParams) {
  beforeEach(async(() => {
     TestBed.configureTestingModule({
        declarations: [SetUpComponent],
        imports: [RouterTestingModule],
        providers: [
           {
              provide: ActivatedRoute, useValue: activatedRouteParams
           }
        ]
     });
  }));
}


describe('SetUp Component:Folder ', () => {
  let component: SetUpComponent;
  let fixture: ComponentFixture<SetUpComponent>;


  configureTestData({
     snapshot: {
        paramMap: convertToParamMap({
           contextType: 'Folder',
           contextId: 'FX6C3F2EDE-DB25-BC3D-0F16-D984753C9D2E',
           contextFriendlyId: 'FX17373'
        })
     }
  });

  beforeEach(() => {
     fixture = TestBed.createComponent(SetUpComponent);
     component = fixture.componentInstance;
     fixture.detectChanges();
  });

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

  it('should create alert with required properties', () => {
     expect(component.contextType).toEqual('Folder);

   .... etc
  });
});

4) router-outlet和routerLink模板文件:

代码语言:javascript
复制
<nav>
  <a routerLink="todos"></a>
</nav>
<router-outlet></router-outlet> 
代码语言:javascript
复制
beforeEach(() => {
  TestBed.configureTestingModule({
     imports: [RouterTestingModule.withRoutes([])],
     declarations: [AppComponent]
  });
});

it('should have router outlet', () => {
  let de = fixture.debugElement.query(By.directive(RouterOutlet));

  expect(de).not.toBeNull();
});

it('should have a link to todos page', () => {
  let debugElements = fixture.debugElement.queryAll(By.directive(RouterLinkWithHref));

  let index = debugElements.findIndex(de => de.properties['href'] === '/todos');
  expect(index).toBeGreaterThan(-1);
});

5)我们可以推送参数component.ts的ActivatedRoute存根

代码语言:javascript
复制
    ngOnInit() {
        this.route.params.subscribe(p => {
            if (p['id'] === 0) {
                this.router.navigate(['not-found']);
            }
        });
    }

规范文件:

代码语言:javascript
复制
class RouterStub {
  navigate(params) {}
}


class ActivatedRouteStub {
  private subject = new Subject();


  get params () {
     return this.subject.asObservable();
  }

  push(value) {
     this.subject.next(value);
  }
}


describe('Navigation Testing', () => {

  beforeEach(() => {
     TestBed.configureTestingModule({
        imports: [],
        providers: [
           {provide: Router, useClass: RouterStub},
           {provide: ActivatedRoute, useClass: ActivatedRouteStub}
        ]
     });
  });

  it('should navigate to invalid page when invalid params passed', () => {
     let router = TestBed.get(Router);
     let spy = spyOn(router, 'navigate');

     let route: ActivatedRouteStub = TestBed.get(ActivatedRoute);
     route.push({id: 0});

     expect(spy).toHaveBeenCalledWith(['not-found'])
  });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58129730

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档