# Writing Tests

## Writing tests

There are rules to write structured test projects:

1. Each module must contains its own tests for angular service, directive, factory, controller, component and filters.
2. Test must be inside test directory of each module.
3. It must have naming convention like *yourservice.\_spec.js or \_yourdirective*.spec.js etc.
4. 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.

1. Ensure invalid emails do not pass.
2. Ensure valid emails pass the validation.
3. Ensure path changes on user logged in.

> AngularJS also has the [`ngMock`](https://docs.angularjs.org/api/ngMock) module, 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`](https://docs.angularjs.org/api/ngMock/service/$httpBackend) , which lets us mock XHR requests in tests, and return sample data instead.

Please open `client/app/login/test/controller.spec.js` file to see all test cases. Files are well documented.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://shoppinpal.gitbook.io/docs-shoppinpal-com/5.-the-frontend/unit-testing/writing-tests.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
