没有指令" exportAs"设置为" ngForm"

时间:2017-01-04 12:25:36

标签: javascript unit-testing angular typescript jasmine

当我尝试测试PhantomJS 2.1.1 (Linux 0.0.0): Executed 3 of 55 (1 FAILED) (0 secs / 0.307 secs) PhantomJS 2.1.1 (Linux 0.0.0) LoginComponent should create FAILED Failed: Uncaught (in promise): Error: Template parse errors: There is no directive with "exportAs" set to "ngForm" ("iv class="col-md-4 col-sm-6 col-md-offset-4 col-sm-offset-3"> <form (ngSubmit)="login(f)" [ERROR ->]#f="ngForm"> <div class="card card-login"> <div class="card-header text-cen"): LoginComponent@5:38

时出现以下错误
login.component.ts

我的组件的一部分,

export class LoginComponent { isBusy: boolean = false; user: User; constructor( private router: Router, private notificationService: NotificationService, private authService: AuthenticationService, private sessionService: SessionService, ) { } .... }

login.component.spec.ts

相应的规格如下,

describe('LoginComponent', () => { let component: LoginComponent; let fixture: ComponentFixture<LoginComponent>; beforeEach(async(() => { let routerStub = new RouterStub(); TestBed.configureTestingModule({ imports: [HttpModule], declarations: [, LoginComponent ], providers: [{ provide: Http, useFactory: (backend, options) => { return new Http(backend, options); }, deps: [MockBackend, BaseRequestOptions] }, { provide: Router, useClass: class { navigate = jasmine.createSpy("navigate"); } }, MockBackend, BaseRequestOptions, AuthenticationService, SessionService, NotificationService ], schemas: [NO_ERRORS_SCHEMA], }) .compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(LoginComponent); component = fixture.componentInstance; fixture.detectChanges(); }); it('should create', () => { expect(component).toBeTruthy(); }); });

FormsModule

似乎无法使用上述提供程序,声明等创建虚假组件。与other's said一样,我已将@NgModule包含在threshold = 10 old_file = 'old_file.txt' with open(old_file, 'r') as document: values = [x for x, value in enumerate(document) if float(value) < threshold] new_file = open('new_file.txt', 'w') for x in values: new_file.write(str(x) + '\n') 装饰器中。

3 个答案:

答案 0 :(得分:21)

不确定您添加了哪个@NgModule装饰器,但我认为它应该添加到

 TestBed.configureTestingModule({
      imports: [HttpModule, FormsModule],

答案 1 :(得分:0)

我遇到了完全相同的问题,我通过在 app.module.ts 和组件所在的子模块(在我的例子中是 core.module.ts)中导入 FormsModule 来解决它。

我遵循 Angular 样式指南并将登录组件放在核心模块中,因此我的登录组件的文件夹结构是:app/src/core/login/login.component.ts

我不需要像其他人建议的那样导入 ReactiveFormsModule。

答案 2 :(得分:0)

被屏蔽为已接受的答案目前已过时,因为 HttpModule 目前已被弃用。另一种方法是使用 HttpClientTestingModule 代替:

 TestBed.configureTestingModule({
      imports: [HttpClientTestingModule, FormsModule],