AngularJS Test Example

All we need is an easy explanation of the problem, so here it is.

I don’t know why but I’m finding it really difficult to get my head around setting up a JavaScript test using AngularJS.

I have my app.js, controller.js plus a number of other files for Ajax, Storage services, filters etc. My app works fine but I want to learn how to do E2E and unit testing.

I think I need to create a standalone HTML page that runs my tests, but I’m not sure, I’m also not sure which extra JavaScript files/libraries I might need.

Is there an AngularJS test example I could use to get the initial set-up?

Ideally I’d like to run the JS tests I create from within Visual Studio 2012…but that’s a nice to have!

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

I find this tutorial a good start.

http://www.yearofmoo.com/2013/01/full-spectrum-testing-with-angularjs-and-testacular.html

It not only teaches how to do unit testing but also E2E and a special one …Midway testing. A good reference IMHO.

Method 2

I recomend you to look at angular-seed project.
It has e2e test already setup.
https://github.com/angular/angular-seed

Method 3

AngularJS dev guide has a complete page on the subject:
http://docs.angularjs.org/guide/unit-testing

There is also a blog post mentioning a sample app in github including Testacular tests here:
http://blog.angularjs.org/2012/11/angularjs-example-applications.html

Hope it helps.

Method 4

Angular Test Patterns is worth looking into as well. There are examples in both CoffeeScript and JavaScript for testing Controllers, Services, Directives and Filters. As well as ideas on how to use Mocks, End-to-End tests etc.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply