我有一个通用基类,用于每个网格组件。
现在,我有网格组件的规范,但我想在单独的基本规范文件中添加基类的规范。
这背后的动机是删除重复的代码,并为基础网格创建通用规范文件,该文件将为使用基类的每个网格提供服务。
我一度陷入困境,我无法在基本规范文件中创建规范。基本上,我希望基文件中的所有规范都在一个函数下,当这个函数从带有该组件的子类中调用时,它应该返回该类的所有规范
以下是我所拥有的以及迄今为止我所尝试的。
组件层:
export class MyComponent extends BaseGrid<MyEntity> {
... (and all other code like constructor and methods)...
}
基层:
export class BaseGrid<T> {
public async getData(): Promise<void> { ... }
}
... and 100 other functions
组件规格:
describe('MyComponent ', () => {
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [MyComponent],
imports: [
...
],
providers: [
],
}).compileComponents().then(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
});
}));
// here I created a reference function for my base class spec
// Now, This is working but I don't want this **describe** and **it** to be here,
// it should be in the base file. so I can remove this repetitive code from all components.
// And From here I just want to call one function let's say a **baseGridSpecs(component)**,
// that will load all the specs of base class in this component.
describe('Should initialize Base grid', () => {
it('should have proper component.', () => {
const baseGridSpecs = new BaseGridSpecs<MyComponent>();
baseGridSpecs.runBaseGridTests(component);
baseGridSpecs.checkGetDataDefined(component);
});
});
});
基本规格:
export class BaseGridSpecs<T> {
runBaseGridTests(component: any): void {
expect(component).toBeTruthy();
}
checkGetDataDefined(component: any): void {
expect(component.getData).toBeDefined();
}
}
这个结构对我来说很好,但它没有用,因为我的describe
和it
仍在主组件规范文件中。
我试图实现的是只调用像
baseGridSpecs.runBaseGridTests(component);
这样的基本规范函数,它应该为给定的通用组件呈现所有describe
和it
规范。
如有任何帮助,我们将不胜感激
Jasmine允许describe
块中的it
定义,除非您从某个describe
块显式调用它们,否则这些定义将不会运行。官方文件提出了两种利用这一事实的方法。
测试主题可以与上下文共享,但它要求您使用function
而不是箭头函数,并且可能很烦人,很难跟踪,尤其是当有许多嵌套块时,因此您为规范创建类的想法是有意义的。
让我们考虑一个包含两个类的简化示例。(游乐场(
// Base.ts
export class Base {
baseMethod() {
return 0;
}
}
和
// Derived.ts
import { Base } from "./base";
export class Derived extends Base {
derivedMethod() {
return 1;
}
}
检查基类部件的派生类测试可以封装在BaseSpec
:中
// base.spec.ts
export class BaseSpec {
public component;
checkComponentInitialized(): void {
it("should have proper component", () => {
expect(this.component).toBeTruthy();
});
}
checkBaseMethodDefined(): void {
it("should have baseMethod method", () => {
expect(this.component.baseMethod).toBeDefined();
});
}
itActsLikeABase(): void {
this.checkComponentInitialized();
this.checkBaseMethodDefined();
}
}
然后这个类可以在其他地方使用
// derived.spec.ts
import { Derived } from "./derived";
import { BaseSpec } from "./base.spec";
describe("Derived", () => {
describe("as a Base", () => {
const baseSpec = new BaseSpec();
beforeEach(() => {
baseSpec.component = new Derived();
});
baseSpec.itActsLikeABase();
});
describe("as a Derived", () => {
let component;
beforeEach(() => {
component = new Derived();
});
it("should have derivedMethod method", () => {
expect(component.derivedMethod).toBeDefined();
});
});
});
免责声明
虽然这在技术上是可能的,在某些情况下可能是合适的,但在以这种方式编写测试之前请三思。继承是一种强大但经常被滥用的工具。由于测试共享行为而导致的重复很可能是意外的重复,因为测试不应该取决于行为的实现方式。共享行为测试警告
因此,在Shalang的帮助下,我能够在我的基本网格规范文件中添加单元测试。以下是我所做的。
组件规格:
describe('MyComponent ', () => {
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [MyComponent],
imports: [
...
],
providers: [
],
}).compileComponents().then(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
});
}));
describe('Should initialize Base grid', () => {
const baseGridSpecs = new BaseGridSpecs<ComponentModel>();
beforeEach(() => {
baseGridSpecs.component = component;
baseGridSpecs.fixture = fixture;
});
// Here for me dummyData is model's object with proper value,
// which I can use to check service based calls.
baseGridSpecs.loadSpecs(dummyData);
});
});
基本规格:
export class BaseGridSpecs<T> {
public component;
public fixture;
loadSpecs(dummyData: any): void {
describe('Should initialize base component with Proper methods', () => {
it('should initialize properly.', () => {
expect(this.component).toBeTruthy();
});
it('should have Base getData function defined.', () => {
expect(this.component.getData).toBeDefined();
});
});
}
}