Angular2 FormBuilder Unit Tests

Chris Pawlukiewicz
Sep 27, 2016 · 1 min read

Hey guys!

If you are using formbuilder, you may wonder, okay, now how do I create tests for my component that uses Formbuilder. Currently, this is how I am accomplishing this.

Suppose you had a small form for your users to login to, with just a user object, a FormGroup with an email and password field, and a function that handles the form submission? How would you test that?

Create a spec, using Angular’s TestBed. Pass FormBuilder as a provider, and make sure to override any services with stubs. Then you should have full access to set the values of your form’s controls, check whether the form is valid, and test other basic FormBuilder functionality.

Here’s some sample code:

Chris Pawlukiewicz

Written by

Novelist, technology freak, software developer, entreprenuer. Founder of kingdomworks —