Javascript 当对角2组件进行单元测试时,内置管道会导致故障

Javascript 当对角2组件进行单元测试时,内置管道会导致故障,javascript,unit-testing,angular,angular-pipe,Javascript,Unit Testing,Angular,Angular Pipe,我有样品组件,我想测试。 当我使用自定义管道(例如我的自定义管道concat)时,它工作正常 import { ConcatPipe } from 'path-to-concat-pipe'; @Component({ selector: 'test', template: '{{ testProp | concat }}' }) class TestComp { public testProp: Array = [2017, 2018]; } 但是当我尝试使用(例如,numbe

我有样品组件,我想测试。 当我使用自定义管道(例如我的自定义管道
concat
)时,它工作正常

import { ConcatPipe } from 'path-to-concat-pipe';

@Component({
  selector: 'test',
  template: '{{ testProp | concat }}'
})
class TestComp {
  public testProp: Array = [2017, 2018];
}
但是当我尝试使用(例如,
number
)时,我的测试失败,没有任何信息性错误消息

@Component({
  selector: 'test',
  template: '{{ testProp | number }}'
})
class TestComp {
  public testProp: number = 2017;
}
样本规格代码

describe('TestComp', () => {
    let comp: TestComp;
  let fixture: ComponentFixture<TestComp>;

  beforeEach(async(() => {
    TestBed
    .configureTestingModule({
      declarations: [TestComp, ConcatPipe],

    })
    .compileComponents()
    .then(() => {
          fixture = TestBed.createComponent(TestComp);        
          fixture.detectChanges();
    })
    }));

  it('TestComp successfully initialized', () => {
    expect(fixture.componentInstance).toBeDefined()
  });
});
karma.conf.js

'use strict';

var webpackConfig = require('./webpack.config');

module.exports = function (config) {
    var _config = {
        basePath: '',

        frameworks: ['jasmine'],

        files: [
            {pattern: './karma-shim.js', watched: false},
            {pattern: './src/app/**/*spec.ts', watched: true, included: false}
        ],

        exclude: [],

        preprocessors: {
            './karma-shim.js': ['webpack', 'sourcemap']
        },

        webpack: webpackConfig,

        webpackMiddleware: {
            stats: 'errors-only'
        },

        coverageReporter: {
            dir: 'coverage/',
            reporters: [
                {type: 'text-summary'},
                {type: 'html'}
            ]
        },

        browserNoActivityTimeout : 100000,

        webpackServer: {
            noInfo: true
        },

        reporters: ['story', 'coverage', 'progress'],

        port: 9876,

        colors: true,

        logLevel: config.LOG_DEBUG,

        autoWatch: false,

        browsers: ['PhantomJS'], 

        singleRun: true,
    };

    config.set(_config);

};

根据文件

我应该向声明中添加哪些类?

将可声明类(组件、指令和管道)添加到 声明清单

这些类必须在 应用如果它们属于此类别,请在此模块中声明它们 模块

因此,您应该导入
CommonModule
,而不是将
DecimalPipe
推送到声明数组:

TestBed.configureTestingModule({
       imports: [CommonModule],
我找到了解决办法。 在PhantomJS浏览器上测试失败,但在Chrome上可以工作。对于生成的项目,同样的问题也可以重现

为了让angular 2内置管道的测试在PhantomJS上运行,应该向其添加2行代码

karma.shim.js如果您使用的是generator-ng2-webpack

src/polyfills.ts在使用angular cli的情况下


谢谢您的回答,但导入CommonModule没有帮助,测试再次失败,您是否从
声明
数组中删除了
DecimalPipe
?是的,
TestBed.configureTestingModule({imports:[CommonModule],DecimalPipe:[TestComp]})
您能在我的插件中复制它吗?谢谢,它在你的plunker中运行良好,即使没有CommonModule导入,但在我的项目中仍然不起作用,我想我的karma配置文件有问题
TestBed.configureTestingModule({
       imports: [CommonModule],
require('intl');
require('intl/locale-data/jsonp/en');
import 'intl';
import 'intl/locale-data/jsonp/en';