Javascript 在角焊缝中测试定制管道

Javascript 在角焊缝中测试定制管道,javascript,angular,formatting,jasmine,Javascript,Angular,Formatting,Jasmine,我正试图为我的定制管道编写一些基本测试,但由于对Jasmine和Angle管道不熟悉,我遇到了一些困难。这是我的烟斗: decimal-format-pipe.js import { Pipe , PipeTransform } from '@angular/core'; import { DecimalPipe } from '@angular/common'; @Pipe({ name: 'myDecimalFormatingPipe' }) export class MyDec

我正试图为我的定制管道编写一些基本测试,但由于对Jasmine和Angle管道不熟悉,我遇到了一些困难。这是我的烟斗:

decimal-format-pipe.js

import { Pipe , PipeTransform } from '@angular/core';
import { DecimalPipe } from '@angular/common';

@Pipe({
    name: 'myDecimalFormatingPipe'
})

export class MyDecimalFormatPipe implements PipeTransform {

    constructor(public decimalPipe: DecimalPipe) {};

    transform(value: any) {
        if (value || value === 0) {
            value = this.decimalPipe.transform(value, '1.2-2');
        }
        return value;
    }
}
显然,这个“自定义”管道目前只是实现十进制管道转换(),但将来会发生变化

这是我的规格:

import { MyDecimalFormatPipe } from './my-decimal-format.pipe';
import { DecimalPipe } from '@angular/common';

describe('MyDecimalFormatPipe', () => {

    let pipe: MyDecimalFormatPipe;
    let decimalPipe: DecimalPipe;
    let inputValue: any = '2.1111';

    beforeEach( () => {
        decimalPipe = new DecimalPipe(inputValue);
        myPipe = new MyDecimalFormatPipe(decimalPipe);
    });

    it('pipe is defined', () => {
        expect(pipe instanceof MyDecimalFormatPipe).toBeTruthy();
    });

    describe('transform ', () => {
        it('should return correct value type ', () => {

            spyOn(decimalPipe, 'transform').and.callThrough();

            decimalPipe.transform(inputValue, '1.2-2');

            expect(decimalPipe.transform).toEqual('2.11');
        });
    });
});
我的第一个规范通过了,但是对于transform()测试,它失败了,我得到了

error: 
 RangeError: Invalid language tag: 2.1111
          at new NumberFormat (native)
          at Function.NumberFormatter.format (
我记不起上次看到这个错误是什么时候了。“无效语言标记”指的是什么?是什么让这个规范打破了它

“无效语言标记”指的是什么

具有用于构造的区域设置的注入字符串参数。尝试
new Intl.NumberFormat('1.222')。format(1.2222)
在浏览器控制台中,您将看到您的错误。

完成
y_vyshnevska
的回答,有几点需要注意:

  • 您需要使用
    decimalPipe=newdecimalpipe('arab')
    告诉
    DecimalPipe
    构造函数使用阿拉伯数字格式(在您的情况下)
  • 从官方文档来看,我相信您不需要在这个测试()中使用spy,但是只要从管道中获取返回的结果就足够了
编辑的部分

beforeEach(() => {
  decimalPipe = new DecimalPipe('arab');
  pipe = new MyDecimalFormatPipe(decimalPipe);
});

...

it('should return correct value type ', () => {
  // spyOn(pipe, 'transform').and.callThrough();
  const result = pipe.transform(inputValue);
  expect(result).toEqual('2.11');
});
...


N.B:我能看到的一件有趣的事情是,使用无头铬合金,测试结果是正确的(2.11);但是在我的chrome浏览器中,如果测试结果不正确,测试就会失败(2,11)。我想这是由于浏览器设置的缘故,但我也没想到:-)

我相信输入值会被解析为日期或任何让它崩溃的东西。你试过使用数值吗?这会改变什么吗?这个测试似乎是正确的。你能详细说明一下“使用数值”是什么意思吗?我目前正在将inputValue中的一个字符串传递给transform(),因为来自后端的JSON将把数字作为字符串…我应该尝试传递一个数字吗?很好,我没有得到这个数字啊!多好的回答啊。我不明白为什么当spyOn.and.callThrough()被删除时,它返回了预期的结果,但在其他方面却没有。据我所知,spyOn只是监视输出和参数…为什么它会更改结果?您应该阅读以下内容:。它很好地解释了为什么在某些情况下需要它,但在这里,您实际上根本不需要它,因为您可以直接调用管道结果!好吧,也许我错过了什么。与Jasmine文档一样,callThrough()遵从实际实现(而不是存根)。我的结论是,
spyOn(decimalPipe,'transform')。和.callThrough()
应该返回与我们在您的实现中简单调用transform()方法相同的结果。然而,我的实现失败了,而你的通行证…嗯,似乎真的很奇怪。。。在您的案例中,
decimalPipe.transform
的日志是什么?