1

My angular2 unit test code fails with the below traceback. I know it's because of not adding formControlName attribute to the input tag which was inside a FormGroup.

But I added [ngModelOptions]="{standalone: true}" to that particular tag.

<md-slide-toggle [(ngModel)]="isAvailable" color="primary" [ngModelOptions]="{standalone: true}">
</md-slide-toggle>

It works perfectly in the main site but on testing, it shows

PhantomJS 2.1.1 (Linux 0.0.0): Executed 39 of 65 (1 FAILED) (0 secs / 1.391 secs)
PhantomJS 2.1.1 (Linux 0.0.0) VehicleFormComponent should create FAILED
    Error: Error in ./VehicleFormComponent class VehicleFormComponent - inline template:41:63 caused by: No value accessor for form control with unspecified name attribute in src/test.ts (line 98781)
    _throwError@webpack:///~/@angular/forms/src/directives/shared.js:122:0 <- src/test.ts:15986:48
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274

1 Answers1

1

As you probably know, the error you are getting if you have not specified

[ngModelOptions]="{standalone: true}

is the following:

enter image description here

So the other option is to set a name attribute. Your current error:

... form control with unspecified name attribute...

seems to be very similar to the error in the above picture. IF it doesn't matter if you are using ngModelOptions or the name attribute, you could try to remove ngModelOptions and insert the name instead. Functionally this doesn't change the execution of your code in any way, therefore it is a very viable option in my opinion (If it works)

AT82
  • 71,416
  • 24
  • 140
  • 167
  • I set name attribute like `` but it shows like `no form control associated with the name isAvaiable`. At last I replace `[ngModelOptions]="{standalone: true}"` with `ngDefaultControl` and it works. – Avinash Raj Feb 07 '17 at 13:29
  • Well great that you figured it out! :) – AT82 Feb 07 '17 at 13:42