Abd*_*eed 6 angular-pipe angular
这是我的date-formatter-by-timezone.pipe.ts管子
import { Pipe, PipeTransform } from '@angular/core';
import { CookieService } from 'ngx-cookie-service';
@Pipe({
name: 'dateFormatterSecByTimezone'
})
export class DateFormatterSecByTimezonePipe implements PipeTransform {
constructor(private cookieService: CookieService) {}
timezone:any = parseInt(this.cookieService.get('TM_Z')) * 1000;
caculateTime(date , timezone){
//some code here ...
}
transform(date: any){
return this.caculateTime(date , this.timezone)
}
}
Run Code Online (Sandbox Code Playgroud)
这是规范文件date-formatter-sec.pipe.spec.ts:
import { DateFormatterSecByTimezonePipe } from './date-formatter-sec-by-
timezone.pipe';
describe('DateFormatterSecByTimezonePipe', () => {
it('create an instance', () => {
const pipe = new DateFormatterSecByTimezonePipe();
expect(pipe).toBeTruthy();
});
});
Run Code Online (Sandbox Code Playgroud)
在规范文件中,我收到此错误:
Expected 1 argument, but got 0.
(alias) new DateFormatterSecByTimezonePipe(cookieService: CookieService):
DateFormatterSecByTimezonePipe
import DateFormatterSecByTimezonePipe
Run Code Online (Sandbox Code Playgroud)
但是当我使用上面那个编辑器建议的代码时,它仍然不起作用!我在我的管道中导入了构造函数,因为我需要在这个管道中使用 cookie 数据。我该如何解决这个错误?
该错误不是来自 Angular,而是一个简单的 Typescript 问题:您有一个带有参数的构造函数,但在测试中您没有传递参数。这个论点通常由 Angular 中的 DI 提供。从测试文档(参见下面的链接):
作为服务消费者,您不必担心这些。您不必担心构造函数参数的顺序或它们的创建方式。作为服务测试人员,您至少必须考虑第一级服务依赖关系,但是当您使用 TestBed 测试实用程序提供和创建服务时,您可以让 Angular DI 进行服务创建并处理构造函数参数顺序。
所以你可以通过使用来修复这个特定的测试
const pipe = new DateFormatterSecByTimezonePipe(null);
Run Code Online (Sandbox Code Playgroud)
但是,一旦您想要编写实际断言管道行为的测试,这就不会很有用。管道本质上就像这里的服务。如果服务不需要依赖项,您可以在测试中自己实例化服务,或者您也可以在测试中构建依赖项。如果你想让Angular使用DI来实例化服务,你需要使用它的工具:
https://angular.io/guide/testing
本文还解释了使用间谍等方法。
| 归档时间: |
|
| 查看次数: |
4397 次 |
| 最近记录: |