模块'DynamicTestModule'声明的意外值'VoteComponent'

时间:2017-09-22 11:21:17

标签: angular

我对Angular /测试很新。正在通过关于测试的Angular文档,我正在努力。已搜索此网站的答案,但一直没有成功。我还尝试在app.module.ts文件中添加其他导入。

使用Jasmine,收到此错误: Error: Unexpected value 'VoteComponent' declared by the module 'DynamicTestModule'

component.ts文件非常基本:

export class VoteComponent { 

  totalVotes = 0; 

  upVote() { 
    this.totalVotes++;
  }

  downVote() { 
    this.totalVotes--;
  }
}

spec.ts文件:(我还没有为downVote()编写代码)

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { VoteComponent } from './vote.component';

describe ('VoteComponent', () => { 
  let comp: VoteComponent;

  beforeEach(() => { 
    TestBed.configureTestingModule({ 
        declarations: [VoteComponent],    
    });

    const fixture = TestBed.createComponent(VoteComponent);
    comp = fixture.componentInstance;

  });

  it('should increment total votes when upvoted', () => { 

    comp.upVote();

    expect(comp.totalVotes).toBe(1); 

  });
});

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

Angular仅尊重@Directive数组中由@Pipedeclarationsimport { Component } from '@angular/core'; @Component({ selector: 'vote-component', template: 'some template' }) export class VoteComponent { totalVotes = 0; upVote() { this.totalVotes++; } downVote() { this.totalVotes--; } } 装饰器装饰的类。

所以你必须向你的班级添加元数据:

public class Loader {

    var xyz:String!

    public class var shared: Loader {
        struct Static {
            static let instance: Loader = Loader()
        }
        return Static.instance
    }
}