我正在尝试在管道的单元测试中模拟服务。我面临的问题是,即使遵循多个指南并使用多种方法,我仍然会在执行测试时遇到import { TestBed } from '@angular/core/testing';
import { DatePipe } from './date.pipe';
import { DateFormat } from '@app/enums';
import { ApiService } from '@app/services';
import { MainService } from '@app/state';
class MockApiService extends ApiService {
transformDateToApi(date: Date = new Date()): string {
return '13-08-1996T22:10:32';
}
}
describe('DatePipe', () => {
const testDate = new Date('13-08-1996T22:10:32');
const testDateAsString = '13-08-1996 22:10:32';
const pipe = new DatePipe();
let apiService: ApiService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: ApiService, useClass: MockApiService },
{ provide: MainService, useValue: {} }
]
});
apiService = TestBed.get(ApiService);
});
// Date object test
it('date (as Date object) will be properly transformed in DateFormat.Xshort format', () => {
expect(pipe.transform(testDate, DateFormat.Xshort)).toBe('13-08-\'96');
});
it('date (as Date object) will be properly transformed in DateFormat.Short format', () => {
expect(pipe.transform(testDate, DateFormat.Short)).toBe('13-08-1996');
});
it('date (as Date object) will be properly transformed in DateFormat.Pretty format', () => {
expect(pipe.transform(testDate, DateFormat.Pretty)).toBe('13 augustus 1996');
});
it('date (as Date object) will be properly transformed in DateFormat.WithTime format', () => {
expect(pipe.transform(testDate, DateFormat.WithTime)).toBe('13 augustus 1996 22:10:32');
});
});
错误。
我见过其他人在组件上执行此任务,但对于Pipe来说,它略有不同,对吗?
date.pipe.spec.ts
import { Pipe, PipeTransform } from '@angular/core';
import { ApiService } from '@app/services';
import { DateFormat } from '@app/enums';
import { DateSegments } from '@app/interfaces';
@Pipe({
name: 'formatDate'
})
export class DatePipe implements PipeTransform {
private date: DateSegments;
constructor (
private apiService: ApiService
) { }
transform(date: Date | string, format: DateFormat = DateFormat.Pretty): string {
if ( date instanceof Date ) {
console.log(this.apiService);
console.log(ApiService);
date = this.apiService.transformDateToApi(date);
}
..... (irrelevant code to this question)
date.pipe.ts
TypeError: Cannot read property 'transformDateToApi' of undefined
我收到的错误是items
。
此时我的试错策略已被带回到将代码粉碎在一起,这个策略并没有教给我任何东西。
答案 0 :(得分:0)
你做错了。
管道非常简单,不应该使用试验台来测试它们。相反,模拟您的服务,只需通过注入它来创建管道。像这样的东西。
import { TestBed } from '@angular/core/testing';
import { DatePipe } from './date.pipe';
import { DateFormat } from '@app/enums';
import { ApiService } from '@app/services';
import { MainService } from '@app/state';
const apiServiceMock = {
transformDateToApi(date: Date = new Date()): string {
return '13-08-1996T22:10:32';
}
};
describe('DatePipe', () => {
const testDate = new Date('13-08-1996T22:10:32');
const testDateAsString = '13-08-1996 22:10:32';
let pipe: DatePipe;
let apiService: ApiService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: ApiService, useValue: apiServiceMock },
{ provide: MainService, useValue: {} }
]
});
apiService = TestBed.get(ApiService);
});
beforeEach(() => {
pipe = new DatePipe(apiServiceMock as any);
});
// Date object test
it('date (as Date object) will be properly transformed in DateFormat.Xshort format', () => {
expect(pipe.transform(testDate, DateFormat.Xshort)).toBe('13-08-\'96');
});
it('date (as Date object) will be properly transformed in DateFormat.Short format', () => {
expect(pipe.transform(testDate, DateFormat.Short)).toBe('13-08-1996');
});
it('date (as Date object) will be properly transformed in DateFormat.Pretty format', () => {
expect(pipe.transform(testDate, DateFormat.Pretty)).toBe('13 augustus 1996');
});
it('date (as Date object) will be properly transformed in DateFormat.WithTime format', () => {
expect(pipe.transform(testDate, DateFormat.WithTime)).toBe('13 augustus 1996 22:10:32');
});
});