【问题标题】:How to write the test cases in angular if there is a init.spec.ts file present?如果存在 init.spec.ts 文件,如何以角度编写测试用例?
【发布时间】:2019-12-25 12:31:24
【问题描述】:

我有一个测试文件夹,里面有 3 个文件,

1)nav-bar.init.spec.ts
2)nav-bar.scaffold.ts
3)nav-bar.spec.ts

我分别有以下代码,

在导航栏.init.spec.ts,

       import { TestBed, async } from '@angular/core/testing';
       import { Navbarcomponent } from './app.component';
        describe('Navbarcomponent', () => {
          beforeEach(async(() => {
                TestBed.configureTestingModule({
              declarations: [
             Navbarcomponent
                ],
              }).compileComponents();
              }));

在 nav-bar.spec.ts,

       import { TestBed, async } from'@angular/core/testing';
       import { Navbarcomponent } from './app.component';
        describe('Navbarcomponent', () => {
           let component: Navbarcomponent;
          beforeEach(() => {
                  component = new Navbarcomponent();
           }));
        it (should create,()=>{
            expect(conponent).toBeTruthy()
        });                 //// I added this test case here

当我运行测试用例时,业力屏幕显示“NO SPEC FOUND”

我的 Karma.config 文件, karma-config-1

 [karma-config-2][2]

我对这种模块化感到困惑。谁能给我建议帮助。谢谢。

【问题讨论】:

  • 你能发布你的 karma.conf.js 吗
  • 确定@Anton Bks
  • @Anton Bks 我用图片更新了业力文件,你能看看吗。
  • 你在使用量角器和因果报应吗?

标签: javascript jasmine mean-stack angular8 angular-unit-test


【解决方案1】:

快速浏览一下,我认为问题在于您没有正确配置测试平台:

import { ComponentFixture, TestBed } from '@angular/core/testing';

describe('AppComponent', () => {
  let component: AppComponent;
  let fixture: ComponentFixture<AppComponent>;

TestBed.configureTestingModule({
      schemas: [NO_ERRORS_SCHEMA],
      declarations: [AppComponent],
      providers: [
        { provide: Router, useValue: routerStub },
        { provide: ActivatedRoute, useValue: activatedRouteStub },
        { provide: I18nService, useValue: i18nServiceStub },
        { provide: PlatformLocation, useValue: platformLocationStub },
        ...
      ]
    });

    fixture = TestBed.createComponent(AppComponent);
    component = fixture.componentInstance;
});

【讨论】:

  • 安东,我做了这些改动
  • 问题是我需要在哪个文件中添加我的测试用例?在 init.spec 或 .spec
  • 测试平台会像你现在拥有的那样进入 init.spec
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-12
  • 1970-01-01
  • 2021-09-13
  • 2020-12-21
  • 1970-01-01
  • 2020-09-28
  • 2020-06-27
相关资源
最近更新 更多