2017-09-15 109 views
0

我是角度測試新手,我想對下面的例子做測試用例。請解釋我如何通過使用karma或jasmine來爲這個登錄函數編寫測試用例?角度測試用例

  ngOnInit() { 
      // reset login status 
      this.authenticationService.logout();   
      // get return url from route parameters or default to '/' 
      this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/'; 
     } 
     login() { 
       this.loading = true; 
       this.authenticationService.login(this.model.username, 
       this.model.password) 
        .subscribe(
         data => {this.router.navigate([this.returnUrl]); 
         }, 
         error => { 
          this.alertService.error(error); 
          this.loading = false; 
         }); 
      } 
+0

看看這個討論:[Angular 2 Jasmine如何測試一個組件的功能](https://stackoverflow.com/questions/40915547/angular-2-jasmine-how-to-test-a-function組件) –

回答

0

要編寫測試用例的角度,你需要做如下:

安裝業力和茉莉 做因緣初始化 使用斷言庫 如需更多幫助,請訪問創建的.spec文件 寫的期望: https://jasmine.github.io/2.8/introduction

2.8是最新版本的茉莉

+0

已經完成了安裝過程。現在,我必須爲該登錄功能編寫測試用例。我只舉幾個例子...爲此。 – Kavitha