Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Angular】单元测试案例 #143

Open
deepthan opened this issue Sep 17, 2021 · 0 comments
Open

【Angular】单元测试案例 #143

deepthan opened this issue Sep 17, 2021 · 0 comments

Comments

@deepthan
Copy link
Owner

单元测试案例

管道

number-to-zh.pipe.ts

import { Pipe, PipeTransform } from "@angular/core";

@Pipe({name: 'numberToZh'})
export class numberToZhPipe implements PipeTransform {
  transform(value: any, ...args: any[]) {
    switch(value) {
      case '1': 
        return '一';
      case '2':
        return '二';
      case '3':
        return '三';
      default: throw new Error(`无效状态码${value}`);
    }
  }
}

将管道在模块中注册下

import { numberToZhPipe } from "./number-to-zh.pipe";

@NgModule({
  ...
  declarations: [numberToZhPipe],
})
export class AppModule {}

然后给其写单元测试

import { numberToZhPipe } from './number-to-zh.pipe';

const pipe = new numberToZhPipe();

it('转换管道应该被创建', ()=>{
    expect(pipe).not.toBeUndefined();
})

it('1应该返回一', ()=>{
    expect(pipe.transform('1')).toEqual('一');
})

it('2应该返回二', ()=>{
    expect(pipe.transform('2')).toEqual('二');
})

it('3应该返回三', ()=>{
    expect(pipe.transform('3')).toEqual('三');
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant