Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

There is no directive with "exportAs" set to "ngForm"

Got the below error when I try to test 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

A part my component looks like,

login.component.ts

export class LoginComponent {

  isBusy: boolean = false;
  user: User;

  constructor(
    private router: Router,
    private notificationService: NotificationService,
    private authService: AuthenticationService,
    private sessionService: SessionService,
  ) { }
  ....
}

And the corresponding spec looks like,

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

seems like it fails to create a fake component using the above providers, declarations, etc. Like other's said, I already included FormsModule inside @NgModule decorator.

like image 253
Avinash Raj Avatar asked Jan 04 '17 12:01

Avinash Raj


People also ask

How do I fix no directive found with Exportas ngForm?

For example, if the export not found is ngForm , we will need to import FormsModule and declare it in our list of imports in *. module. ts to resolve the missing export error. content_copy import { FormsModule } from '@angular/forms'; @NgModule({ …


1 Answers

Not sure what @NgModule decorator you added it to but I think it should be added in

 TestBed.configureTestingModule({
      imports: [HttpModule, FormsModule],
like image 153
Günter Zöchbauer Avatar answered Oct 12 '22 11:10

Günter Zöchbauer