Quick Start?

To run the sample, open a command prompt and execute the following commands:

  • npm install (to install karma package)
  • npm install -g gulp (to install gulp globally)
  • npm install -g karma-cli (to install karma CLI globally)
  • bower install (to install front-end packages)
  • gulp test (to start karma and run tests / Run test once and exit)
  • gulp dev-test (to start karma and run tests / Watch for file changes and re-run tests on each change)
  • npm test (to start karma and run tests / Watch for file changes and re-run tests on each change)

About Unit Testing:

The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remaining of the code, and determine whether it behaves perfectly as you expect.

Testing AngularJS Application :

AngularJS build around the concept of dependency injection & loose coupling to the DOM which make it testable.

Test-Driven Development: Test-Driven Development a software development process that relies on the repetition of a short development cycle: first the developer writes an (initially failing) automated test case that defines a desired improvement or new function, then produces the fair amount of code to pass that test, and lastly refractors the new code to acceptable standards.

AngularJS

AngularJS is one of the most popular single-page application frameworks.

Why Karma?

Karma is a javascript command-line tool that used to spawn a web server that loads your application.

The main goal for Karma is to bring a productive testing environment to developers. The environment is one where they don’t have to set up loads of configurations, but rather a place where developers can write the code and get instant feedback from their tests. Because getting quick feedback is what makes you productive and creative.

http://karma-runner.github.io

Why Jasmine?

Jasmine is an open-source behaviour-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can simplify writing tests.

  • Jasmine provides functions to help with structuring your tests and also making assertions.

For more details go to the following link:

https://github.com/vishwackh/AngularJS-Jasmine-Karma/wiki/About-Jasmine

http://jasmine.github.io

Why ANGULAR MOCKS?

ANGULAR MOCKS Allows you to inject and mock Angular services for unit tests.

https://docs.angularjs.org/guide/unit-testing

Project Setup:

— Setting up our TDD Environment Project Setup —

  • Let`s start writing from scratch. First, we need to create a basic project structure with a folder that contains our sources, and a folder that contains our specs. then we can init a package.json using the command $npm init
  • $ npm install karma let`s install Karma (and all the plugins your project needs) locally in the project’s directory. (don`t forget to use the — save-dev flags)
  • $ npm install karma-jasmine $ npm install karma-chrome-launcher since we going to use jasmine, let`s install the karma adaptor plugin for jasmine. we will also install the chrome launcher plugin to enable karma to launch chrome browser for us.
  • $ npm install -g karma-cli, we will install the karma command-line interface (CLI) globally, which enable us to simplify and configure karma in our project
  • $ karma init with the karma CLI installed, we can create our configuration file fast and easy.
  • $ karma start $ karma run Let`s take karma for a test drive: in web storm, right-click on the configuration file and choose Run. if you don`t use the web store, start the karma server with start, and run your tests with run (karma start/karma start karma.conf.js)

a quick introduction to the Jasmine framework Jasmine

In jasmine, we begin by creating a test suite with the global describe function that wrap our specs. specs defined by the global function it. inside the spec, we can describe our expectations by using the expect function chained to a matcher function

we can run code before and after each spec in a suite block using the beforeEach and afterEach functions

jasmine use spies to track calls to a function with all its arguments. The toHaveBeenCalled and The toHaveBeenCalledWith special matchers for interacting with spies.

Testing AngularJS

Test-Driven Development in practice

We are going to develop a small app. in the process we will learn how to unit test the building blocks of every AngularJS application: controllers, services, directives, events & HTTP requests. First, let`s get some resources using bower

Controller

To test controllers we need to hold an instance of the controller, initialize a scope for it and testing our expectations against that scope.

Services

To test services we need to use the $injector to get an instance of the service

Directive

To test a directive, we need to create an element that will host the directive and compile it with a scope. in our spec, we need to trigger the digest.

HTTP requests

the $httpBackend is a fake HTTP Back-end implementation. in the most basic use we can verify that a request is made & stub responses

Productive Tips

— Making testing even easier —

When the number of test suites and specs grows larger, the overall test speed is affected. jasmine include some useful syntax to control it.

web store users can install the ddescriber for jasmine(https://plugins.jetbrains.com/plugin/7233-ddescriber-for-jasmine/)

Sample Examples with explanation

Reference

--

--

--

I am a Master's Cybersecurity student in the Netherlands and currently writing my thesis. Also, I have obtained MSc honors in Change Leadership and Management.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Player Damage VFX

Splitting Javascript Classes Into Different Files

Animate it Like Apple

🎉Prizes for @PoloniDEX & @jsbfoundation ($JSB) Trading Competition have been distributed to the…

Results of 4000 PXP Giveaway for Thanksgiving Day from PointPay

Presenter First (MVP) using TypeScript (Javascript)

Step-by-step guide for implementing API Authentication with JSON Web Tokens (JWT)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akshay Prasad

Akshay Prasad

I am a Master's Cybersecurity student in the Netherlands and currently writing my thesis. Also, I have obtained MSc honors in Change Leadership and Management.

More from Medium

Usage of HMS Kits in Travel and Transport Applications

Loops and Nested Loops

Say Hello to SQLDelight