AngularJS unit testing with Jasmine

I am currently working on a hybrid mobile app using Ionic which is based on AngularJS. AngularJS, just like my favorite ActionScript 3 framework Robotlegs, was designed to be testable. With helps of dependency injection and built in mocks, it is quite easy to write tests but still can be painful to get everything sorted out. Here are some tips to make life easier. Like a template (in WebStorm) to setup a test for a service:

1
2
3
4
5
6
7
describe('$MODULE$.$NAME$', function () {
var $NAME$;
beforeEach(module('$MODULE$'));
beforeEach(inject(function (_$NAME$_) {
$NAME$ = _$NAME$_;
}));
});

Mock dependencies

At start of tests when we load the module we can mock dependencies at the same time:

1
2
3
4
5
var $VALUE$;
beforeEach(module('$MODULE$', function ($provide) {
$VALUE$ = {$END$};
$provide.value('$VALUE$', $VALUE$);
}));

Test controllers

Unlike services, controllers can not be injected so we need to instantiate them ourselves.

Read More

C++ unit testing with AppCode and GoogleTest

AppCode started to support Google Test since 2.5 EAP. Steps to get it work:

  1. Get Google Test
  2. Open {gtest_path}/xcode/gtest.xcodeproj and build gtest.framework
  3. Create a “Command Line Tool” project or target
  4. Add gtest.framework to project and the new target
  5. Set Up the Executable Run Environment. 3 options:
  6. Make sure your project and google test use the same C++ standard library

    Read More

TDD Demo GameOfLife with Android Studio

On my Java class the teacher used Conway’s Game of Life as an example for TDD and I found it a really good example. So I made some videos to demonstrate how I tried TDD on it. In the videos when I say “grid” or “gred” it should actually be “cell” (And I pronounced “live” wrongly). Sorry for my poor English. If the videos are not clear please try to change quality to HD by the settings button.

For each step I also posted new or changed code below, and links of full code after each step on GitHub. I posted some keyboard shortcuts as well. I don’t know what they are on windows but you can find them (and change them if you would like) in “Keymap” section of Android Studio’s preference (or settings) panel.

1. Create GameOfLife Project

The first step simply create a new project on Android Studio. Android Studio is still in beta (I’m using 0.8.6) but I am very happy with it so far. On the GDG Auckland September Meetup, Julius Spencer said his team (JSA) has already been using Android Studio on production for one year now. So give it a try if you haven’t yet. It’s so much better than eclipse!

Read More