There are rules to write structured test projects:
- Each module must contains its own tests for angular service, directive, factory, controller, component and filters.
- Test must be inside test directory of each module.
- It must have naming convention like yourservice._spec.js or _yourdirective.spec.js etc.
- And most important of all do not forget to include the test directory in karma.conf file :)
Lets first write tests for login module in angular app. For simplicity of code I am taking three test cases for it.
- Ensure invalid emails do not pass.
- Ensure valid emails pass the validation.
- Ensure path changes on user logged in.
AngularJS also has the
ngMockmodule, which provides mocking for your tests. This is used to inject and mock AngularJS services within unit tests. In addition, it is able to extend other modules so they are synchronous. Having tests synchronous keeps them much cleaner and easier to work with. One of the most useful parts of ngMock is
$httpBackend, which lets us mock XHR requests in tests, and return sample data instead.
client/app/login/test/controller.spec.js file to see all test cases. Files are well documented.