Skip to content

2016

Unit testing in Angular2

Unit tests in Angular2 with Karma

To follow up on my first article about Angular2 I want to unit test our Login component:

import {Component} from 'angular2/core';
import {AuthenticationService} from '../services/authentication';
import {Router} from 'angular2/router';

@Component({
  selector: 'login',
  providers: [ ],
  directives: [ ],
  pipes: [ ],
  styles: [ require('./login.css') ],
  template: require('./login.html')
})
export class Login {
  private credentials: string = '';

  constructor(private authService: AuthenticationService, private router: Router) {
  }

  onSignIn(){
    this.authService.setCredentials(this.credentials);
    this.router.navigate(['Home']);
  }
}

Creating a singleton Service in Angular2

Motivation, building a licencing application

I already use AngularJS for the frontend of OctoPerf, a load testing solution. I will need to migrate my application from AngularJS to Angular2 in a few months.

To get ready for it I created a licenses management application. I started from the excellent angular2-webpack-starter.

The sample AuthenticationService

I quickly needed to create a singleton service and found out that it's not as straightforward as in AngularJS.

Let's say we need an AuthenticationService that holds the user credentials. It is used in the login page to store it, and in the home page to retrieve the information.

Test mobile native applications with OctoPerf

As mobility has been the trend these past years, all the users coming to a website do so from many different devices. OctoPerf allows you to simulate traffic to your website, but it also allows to simulate this kind of multi-device traffic.

That being said, to take advantage of OctoPerf possibilities, you might need to import several different user journeys. Because a mobile user is very likely to deal with a different version of the application. Thus if you record only one user journey from a computer's browser and use it for your tests, they may not be realistic.

Browser based mobile apps

To record the traffic from a mobile application, you have several options. For browser based apps, you can record them from Chrome or firefox and just use the dev tools to switch the user agent:

Native apps are a bit more complex to get by.

Let's see what is required step by step to record such an application.

Are you buying Quality software?

In our previous article, Building Better Software, we have already shown how clean we try to maintain our codebase. We strongly believe that it's possible to run a successful business and have clean code at the same time. While it's relatively easy to show some nice metrics a single time, it's much hard to keep your code clean over time.

Even if we release some major features like On-Premise load testing, we still follow the boy scout's rule: Always leave the campground cleaner than you found it. Clean Code is not only compatible with business needs, it's almost mandatory if you want to be quickly competitive on a given market.

Unlike most software companies who don't really care about the quality of the software they ship, we do care that you get the best bang for the buck load testing tool. Do you know any other software company which regularly publishes code quality metrics? Probably not. We're trying here to introduce a new movement in favor of clean code.

What is Clean Code

Clean code

Clean code focus developing software that's easy to read, maintain and evolve. Good programmers have quickly understood that we are writing code for humans, not computers. Dikjstra, a famous mathematician, told something very interesting in 1972 at the Turing Award Lecture: