如何测试ActivatedRoute

时间:2018-10-17 08:59:26

标签: angular unit-testing testing

我对Angular很陌生。我正在尝试提高代码覆盖率,而且似乎无法通过 this.route.snapshot.paramMap 来获取“用户名”的值。这是我的课。运行测试时,即使设置了component.username ='test@example.com',我仍然得到的消息是需要用户名值; 请协助

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Constants } from '../../app.constants';
import { AuthService } from '../../services/auth/auth.service';
import { AlertService } from '../../services/alert/alert.service';
import * as _ from 'underscore';

@Component({
  selector: 'app-activate',
  templateUrl: './activate.component.html',
  styleUrls: ['./activate.component.scss']
})
export class ActivateComponent implements OnInit {

  status: string;
  error: string;
  username: string;
  token: string;

  constructor(private route: ActivatedRoute, private authService: AuthService, private alertService: AlertService) { }

  activate() {

    this.username = this.route.snapshot.paramMap.get('username');
    this.token = this.route.snapshot.paramMap.get('token');

 if (_.isEmpty(this.username)) {
  this.error = 'A username value is required '
} else if (_.isEmpty(this.token)) {
  this.error = 'A token value is required '
}
}
ngOnInit() {
this.activate();

}

这是我的测试

fdescribe('ActivateComponent', () => {
let component: ActivateComponent;
let fixture: ComponentFixture<ActivateComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
  declarations: [ ActivateComponent ],
  imports: [ RouterModule, RouterTestingModule, HttpClientTestingModule, MatDialogModule ],
  providers: [ AuthService, AlertService, ActivatedRouteMock,]
})
.compileComponents();
}));

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

it('should check if username and token have been filled out', () => {
component.username = 'test@example.com';
component.token = '12345';
console.log(component.username);
// expect(component).toBeTruthy();
});
});

1 个答案:

答案 0 :(得分:0)

已解决,需要在提供程序中添加以下内容:

   beforeEach(async(() => {
   TestBed.configureTestingModule({
   declarations: [ ActivateComponent ],
   imports: [ RouterModule, RouterTestingModule, HttpClientTestingModule, 
    MatDialogModule ],
   providers: [ AuthService, AlertService , ActivatedRouteMock,
    {
      provide: ActivatedRoute,
      useValue: {
        snapshot: {
          paramMap: convertToParamMap({
            username: 'test@example.com',
            token: '23435'
          })
        }
      }
    }]
  })
  .compileComponents();
  }));

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

  it('should check if username and token have been filled out', () => {
 // component.username = 'test@example.com';
 // component.token = '12345';
 // TestBed.get(ActivatedRoute);
 console.log(component.username);
 console.log(component.token);
 // expect(component).toBeTruthy();
 });
 });