AngularJS and unit testing: Jasmine, Karma and mocks

I spent some time a while back getting up to speed with unit test approaches with AngularJS, but it’s been a while and I need to retrace my steps to pick it up again.

For (my) future reference, here’s a list of the notes I had before:

To pick this up again, here’s my reading list of some useful articles to review:

MongoDB aggregation query with a $sort

I have an aggregation query to count documents grouped by a property (this is actually part of my http://www.spotviz.info app that I’m working on – the query retrieves counts per Amateur Radio callsign for number of spots uploaded).

By default the result are not in any particular order (that I can see, but maybe I don’t enough enough test data to be able to tell), so I wanted to add a $sort condition.

Here’s the initial aggregation query:

db.Spot.aggregate(
[
{ $group : { _id : {"spotterCallsign" : "$spotter"},
count : {$sum : 1},
firstSpot : {$min : "$spotReceivedTimestamp"},
lastSpot : {$max : "$spotReceivedTimestamp"} }
}
] )

To add the $sort to the aggregation pipeline, just add another document for $sort following the $group, like this:

{ $sort : { "count" : -1 } }

The full query now looks like:

db.Spot.aggregate(
[
{ $group : { _id : {"spotterCallsign" : "$spotter"},
count : {$sum : 1},
firstSpot : {$min : "$spotReceivedTimestamp"},
lastSpot : {$max : "$spotReceivedTimestamp"} }
},
{ $sort : { "count" : -1 } }
] )

Building this query with the Java API is easy, just add another DBObject for the $sort document to the List containing all docs ($group, $sort), in the pipeline:

DBCollection col = db.getCollection("Spot");

// $group
DBObject groupFields = new BasicDBObject("_id", "$spotter");
groupFields.put("firstSpot", new BasicDBObject("$min", "$spotReceivedTimestamp"));
groupFields.put("lastSpot", new BasicDBObject("$max", "$spotReceivedTimestamp"));
groupFields.put("totalSpots", new BasicDBObject("$sum", 1));
DBObject group = new BasicDBObject("$group", groupFields);

List<DBObject> pipeline = Arrays.asList(group,
new BasicDBObject("$sort", new BasicDBObject("totalSpots", -1)));

AggregationOutput output = col.aggregate(pipeline);

AngularJS console errors are links to further details (?!)

I don’t know why I didn’t notice or try this before, but I noticed in the Udemy ‘AngularJS Zero to Hero’ course that the instructor was clicking the links in the console errors. Previously I thought those rather oddly looking random character messages in the console were just not particularly friendly, but now they make more sense.

Maybe this is completely obvious to others, but this is a very neat feature that I had completely not picked up on.  The links are parameterized with details of your specific error, and the error message page in the AngularJS docs gives you more specifics about the error.

For example, if I have a typo in my ng-app:

Clicking on the link in the error message takes you to a detailed page with further details:

Managing web dependencies with bower

Some rough notes on Bower to remember for future usage:

  • bower install name : install dependency ‘name’
  • bower list : list current project dependencies
  • bower search name : search for a module in the repo
  • bower update name : update name to latest
  • bower update name#1.1 : update name to 1.1

Getting started ref here.