Writing Unit Tests for Angular Components

Browser-based acceptance tests are slow and brittle. Even though we’ve eschewed starting an actual browser for each test by using PhantomJS, we still have to start our server and have it serve pages to the headless browser. Further, our tests rely on DOM elements and CSS classes to locate content used to verify behavior. We may need (or want) to make changes to the view that don’t break functionality but break our tests.

Although we don’t want to abandon acceptance tests—after all, they are the only tests we have that exercise the system end to end—we need a way to test isolated bits of functionality (commonly called unit tests). In Rails, we have model tests and controller tests that allow us to do ...

Get Rails, Angular, Postgres, and Bootstrap now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.