javascript - 没有将 "exportAs"设置为 "ngForm"的指令

标签 javascript unit-testing angular typescript jasmine

当我尝试测试 LoginComponent 时出现以下错误

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();
  });
});

似乎无法使用上述提供程序、声明等创建假组件。如 other's said ,我已经在 @NgModule 装饰器中包含了 FormsModule

最佳答案

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

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

关于javascript - 没有将 "exportAs"设置为 "ngForm"的指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41463799/

相关文章:

javascript - 这个javascript有什么问题?数组未定义

java - @BeforeClass 注释在动态创建的测试套件中不起作用

Python 从导入的模块中模拟一个函数

angular - 重定向到 Angular 2 中第一个允许的路由?

javascript - 正则表达式不适用于 IE,但适用于其他浏览器

javascript - 在javascript中将对象存储在数组中就是存储函数

angular - 没有 TemplateRef 的提供者! (NgIf ->TemplateRef)

angular - 类型 void 不可分配给类型 any

javascript - Zapier 编写的代码 - 用整数分割字符串和带有多个分隔符的文本

reactjs - 为什么在我使用 React 测试库的测试中没有执行 jest 模拟函数?