I have a side project called Proverb. It doesn't do anything in particular - for the most part it's a simple application that displays the collected wise sayings of a team that I used to be part of. There's not much to it - a bit of CRUD, a dashboard. Not much more. Because of the project's simplicity it's ideal to use Proverb's underlying idea when trying out new technologies / frameworks. The best way to learn is to do. So if I want to learn "X", then building Proverb using "X" is a good way to go.
Whilst I'm at it I thought it might prove helpful if I wrote up how I approached writing unit tests for a single Angular controller. So here goes.
I have an Angular controller called
sagesDetail. It powers this screen:
sagesDetail is a very simple controller. It does these things:
- Load the "sage" (think of it as just a "user") and make it available on the controller so it can be bound to the view.
- Set the view title.
- Log view activation.
- Expose a
gotoEditmethod which, when called, redirects the user to the edit screen.
The controller is written in TypeScript and looks like this:
I have put very comments in the test code to make clear the intent to you, dear reader. Annotated the life out of them. Naturally I wouldn't expect a test to be so heavily annotated in a typical test suite - and you can be sure mine normally aren't!