首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Karma正在加载页面而不是测试结果

Karma正在加载页面而不是测试结果
EN

Stack Overflow用户
提问于 2017-08-25 17:08:56
回答 1查看 513关注 0票数 2

最近,我们开始在Angular 4中重写应用程序的前端,我的任务是专注于使用jasmine和karma进行单元测试。我对这个框架和测试运行程序是完全陌生的,而且主要是对整个前端的测试,所以如果我错过了一些非常重要的东西-很抱歉。所以我的问题是测试html输出。当我运行我的测试时,我通常会得到一些信息,关于哪些失败了,发生了什么类型的失败--这很好。从那时起,我一直在做测试,最终让其中一些最简单的东西能够工作。现在我开始处理一个组件活动,它需要一些简单的mock。这是我的spec.ts

代码语言:javascript
运行
复制
import { WelcomeComponent } from "app/core/welcome/welcome.component";
import { ComponentFixture } from "@angular/core/testing";
import { TestBed } from "@angular/core/testing";
import { async } from "@angular/core/testing";
import { BusinessAreasService } from "app/core/services/business-areas/business-areas.service";
import { CoreModule } from "app/core/core.module";
import { Router } from "@angular/router";
import { DebugElement } from "@angular/core/src/debug/debug_node";
import { By } from "@angular/platform-browser";


describe('WelcomeComponent testing', () => {

  let component: WelcomeComponent;
  let fixture: ComponentFixture<WelcomeComponent>;
  let de: DebugElement;
  let el: HTMLElement;

  let mockBusinessAreasService;
  let mockRouter;


  beforeEach(async(() => {

    mockBusinessAreasService = {};
    mockRouter = {};

    TestBed.configureTestingModule({
      providers: [
        {
          provide: BusinessAreasService, useValue:     mockBusinessAreasService
        },
        {
          provide: Router, useValue: mockRouter
        }
      ],
      declarations: [WelcomeComponent]
    }).compileComponents();

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

    component.ngOnInit();

    mockBusinessAreasService = TestBed.get(BusinessAreasService);
    mockRouter = TestBed.get(Router);
  });

  it('BA list shouldnt be empty'), () => {
    expect(component.baList.length).toBeGreaterThan(0);
  }

  it('check if WelcomeComponent is created', () => {
    expect(component).toBeDefined();
  });
});

现在,我的包含WelcomeComponent的.ts文件:

代码语言:javascript
运行
复制
import { Component, OnInit } from '@angular/core';
import { BusinessAreasService } from "app/core/services/business-areas/business-areas.service";
import { Router } from "@angular/router/";

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

  baList : string[] = [];

  selectedBa : string = "";

  constructor(private businessAreasService : BusinessAreasService, private router: Router) { }

  ngOnInit() {
    console.log("In welcome component")
      this.businessAreasService.getUserBAList().subscribe(resposne =>     this.redirectBasedOnBas(resposne), error => this.baList = [ 'error' ]); 
  }

  redirectBasedOnBas(baList : string[]) {
    if(baList.length === 1) {
      this.applyBusinessArea(baList[0]);
    } else {
      this.baList = baList;
      this.selectedBa = baList[0];
    }
}

  baChanged(ba : string) {
    console.log(ba);
    this.selectedBa = ba;
  }

  applyBusinessArea(ba : string) {
    localStorage.setItem('ba', ba);
      this.router.navigate(['']);
  }
}

这是我的HTML文件:

代码语言:javascript
运行
复制
<div class="container-fluid">
  <div class="col-sm-2">
    <label for="name" class="cols-sm-2 control-label">Please select your business Area</label>
  </div>
  <form class="form-inline text-md-center">
    <div class="col-sm-6">
    <div class="input-group">
        <select class="form-control" id="exampleSelect1" (change)="baChanged($event.target.value)">
                <option *ngFor="let ba of baList" [value]="ba">{{ba}}    </option>
        </select>
      </div>
    </div>
    <div class="col-sm-3">
      <button type="button" class="btn btn-primary btn-block"     (click)="applyBusinessArea(selectedBa)">Submit</button>
    </div>
  </form>
</div>

这是我的coreModule,我们没有任何用于欢迎窗口的专用模块,

代码语言:javascript
运行
复制
import { NgModule, Optional, SkipSelf, OnInit } from '@angular/core';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';
import { EnvironmentService } from "app/core/services/environment/environment.service";
import { HttpAdapterService } from "app/core/services/http-adapter/http-adapter.service";
import { AuthentificationService } from "app/core/services/authentification/authentification.service";
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from "@angular/forms";
import { WelcomeComponent } from './welcome/welcome.component';
import { BusinessAreasService } from "app/core/services/business-areas/business-areas.service";
import { FakeLoginComponent } from "app/core/fake-login/fake-login.component";
import { SharedModule } from "app/shared/shared.module";
import { SystemSettingsService } from "app/core/services/system-settings/system-settings.service";

const SERVICES = [EnvironmentService, HttpAdapterService,     AuthentificationService, BusinessAreasService, SystemSettingsService];
const COMPONENTS = [ FakeLoginComponent ];


@NgModule({
    imports: [RouterModule, BrowserModule, FormsModule, SharedModule],
    providers: [SERVICES],
    declarations: [ WelcomeComponent, COMPONENTS ],
    exports: [HttpModule, COMPONENTS]
})
class CoreModule implements OnInit {

    constructor( @Optional() @SkipSelf() parentModule: CoreModule) {
        if (parentModule) {
            throw new Error('CoreModule has been already loaded! It should be loaded only once!');
        }
        console.log('CoreModule contruct');
    }

    public ngOnInit(): void {
        console.log('CoreModule init');
    }
}

export { CoreModule };

所以问题出在这里--当我运行测试时,我会在浏览器中得到失败的堆栈/正的输出,我得到的是:

我打赌问题出在我的配置测试模块中,因为modyfing没有显示欢迎文件的html输出,而是失败的堆栈跟踪。然而,我所想要的就是让这个测试通过。有人能帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2017-08-25 17:14:12

在你的测试中,你运行"WelcomeComponent“的ngOnit,在那里你调用一个函数,一直到这个方法:

代码语言:javascript
运行
复制
  applyBusinessArea(ba : string) {
localStorage.setItem('ba', ba);
  this.router.navigate(['']);  }

现在问题出在这里,你在测试中喋喋不休地说:“不管上面有什么都会显示出来。”

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45878036

复制
相关文章

相似问题

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