Difficulty Approaching Test Driven Development in AngularJS using Jasmine and Karma Difficulty Approaching Test Driven Development in AngularJS using Jasmine and Karma angularjs angularjs

Difficulty Approaching Test Driven Development in AngularJS using Jasmine and Karma


FWIW, you could walk through my sample code for one of my Pluralsight courses. This was the first AngularJS project I ever wrote, and I used Test-Driven Development with Jasmine and Karma.

To help me with my own thought process, I created many small commits, and whenever something interesting happened, I attempted to document it in my commit messages. You might want to review the commits to get an idea about how I went through it.

Since I based my code base on angular-seed, the first many commits in my repository are the commits from angular-seed. My first commit is this one.

There are probably many mistakes in my code base, as I was learning as I progressed, so I'm not claiming that this is exemplary AngularJS code, but I'm not too unhappy about it.


I'm absolutely willing to be corrected on this, as I'm not an expert, but my experience is that it is actually easier to start with the router, because I often know more about the routes the app is going to have to offer than anything else.

Each route needs a controller and a template. So whatever is is true, I'm going to have a controller for each of the routes. Thus, I can quickly work up tests for each controller. Not that the tests will test much at first (maybe just the existence of the controllers.) You can easily write a test for the router as well.

Then maybe take one controller at a time, writing tests for the behavior each is expected to implement, then implementing the behavior.

At some stage, it will probably be best to abstract some of that behavior out of controllers, refactoring it into services. Then the services can be tested.


I am new to writing AngularJS apps as well, but I follow a strict TDD cycle in the code I develop. I found Angular to be one of my favorite JS frameworks for doing testing because it seems to be designed around the idea of testing.

Personally I start with an end-to-end test which may touch real account information (not a live production account though) which describes the feature I need to build from a high level. After I feel comfortable with the higher level test I will begin creating tests for the model. I try to keep little logic in controllers or views so my models have the most tests which are verified by end-to-end tests.

I like the layout Mark mentioned above from his code. I wrote a little about my experiences using AngularJS in a TDD cycle.