#The importance of TDD: Testing Angular
The app is one of my previous project. The main aim of this exercise is to learn how to:
- Test with Karma;
- Unit test Controllers;
- Unit test Services;
- Unit test Filters;
- Unit test Directives;
- E2E testing.
===============================================
##Test with Karma
Karma is a testing automation tool, before known as Testacular (keep this in mind when looking for documentation in the web).
To use Karma you need to install three modules locally :
- karma itself
npm install karma
- karma chrome launcher
npm karma-chrome-launcher
- karma-jasmine
npm karma-jasmine
and one module globally that will allow us to run karma from the command line.
- karma command-line-interface
npm install karma-cli -g
After having installed all these packages, you need a configuration file. Just type:
karma init
Then, just run Karma specifying the folder in which the config file is:
karma start test/karma.conf.js
Be aware that the config file has to be accurate or your tests won't run. See this link for more information.
In order to test different services, I have then used ngMock. To have ngMock, run the following command:
npm install --save-dev angular-mocks
Then you need to edit the files array from the karma.conf.js
to include ngMock
.
files: [ 'node_modules/angular-mocks/angular-mocks.js'. './src/**/*.js', './test/**/*.js' ]
##Testing a Service
##Routing
In this really trivial project I have included also routing and how to test $route in Angular.
index.html
will be your SPA- In
index.html
you will need to includeng-view
. template
folder in home route (hint: remember difference between templates and views).- Refactor your html in the template folder.
- You don not need to specify your controller anymore.
- IMPORTANT STEP: you need to change your module .You need to insert the
.config
and keep in mind that will still return a module hence, you can still concatenate your factory methods. - Inject all the new dependencies (check the version of angular you are actually using).
##Using MongoDB This is the most difficult part of the project. The aim is to save the user's input from the 'Create a New Event' form and save the input in a db.
You first need to have mongo db installed on your machine. Then you need to install mongoose by typing npm install mongoose
. Add this to your server:
var mongoose = require('mongoose');
with this command you are actually setting the connection. After that you need to tell to your mongoose object where to look for a connection. You do that by typing:
mongoose.connect('mongodb://localhost/my_database');
where my_database
is the name of the database you want to be connected to. You can check if you are actually connected to the db by adding this to your server:
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function (callback) {
// yay the database is connected!
});
After those steps need to create your Schema document and with Mongoose everything is derived from a Schema. The Schema is the document that contains the caracteristics of your database entries. An example might be:
var mySchema = mongoose.Schema({
name: String
})
Remember then to export your schema with module.exports = mongoose.model('PartyEvent', partySchema);
.