在测试$ componentController时,typescript会抱怨调用签名

时间:2017-04-14 18:44:27

标签: angularjs typescript webpack jasmine tdd

我正在尝试测试角度1.5的组件,但是Typescript在控制台中抛出红色。

  

(17,27):错误TS2349:无法调用类型缺少调用签名的表达式。类型“IComponentOptions”没有兼容的呼叫签名。   PhantomJS 2.1.1(Windows 8 0.0.0)组件:RecipeContainer应该有一个已定义的组件FAILED

我是否错误地包含了这些文件? (我正在使用webpack编译)。

我错过了类型定义吗?

任何帮助都会受到赞赏,现在已经好几个小时了。

规范文件:

import * as angular from 'angular';
import 'angular-mocks';

import { RecipeContainer } from './recipe-container.component';
import { recipes } from '../recipe-store';

describe('Component: RecipeContainer', () => {
    let $componentController: ng.IComponentOptions;

    beforeEach(angular.mock.module('app'));
    beforeEach(angular.mock.inject((_$componentController_: ng.IComponentOptions) => {
        $componentController = _$componentController_;
    }));

    it('should have a defined component', () => {
        const bindings = { recipes };
        const component = $componentController('RecipeContainer', null, bindings); //webstorm underlines this line 
        expect(component).toBeDefined();
    });
});

component.ts:

class RecipeContainerController implements ng.IComponentController {
    constructor(private $log: ng.ILogService) {
    }
    $onInit() {
        this.$log.info('inside onInit');
    }
}

export const RecipeContainer: ng.IComponentOptions = {
    bindings: {
        recipes: '<'
    },

    controller: RecipeContainerController,

    template: '<div>hello</div>'
};

1 个答案:

答案 0 :(得分:1)

$componentControllerIComponentControllerService type。不是IComponentOptions。如果在变量上使用了错误的类型,则可以预期TypeScript类型系统会抱怨。

可能应该是

beforeEach(angular.mock.inject((_$componentController_: ng.IComponentControllerService) => {
  ...