Tuesday, 13 August 2013

Using jasmine to test JQuery

Ive been doing lots of javascript work of late (mainly in a durandal SPA app) and wanted to test the following function that uses jquery.

function update () {
    errorVisibleFlag(false);
    $("input.delete:checkbox").each(function() {
        if ($(this).is(":checked")) {
            var idToDelete = $(this).attr("id"),
                status = $(this).attr("status");
            dataService.delete(idToDelete)
                .fail(function(){errorVisibleFlag(true)});
        }
    });
    get();
}

This function is part of a knockout view model within a durandal app.

I came up with the following jasmine test that uses spys to verify that the calls were working and also some DOM manipulation to allow jquery to bind the function to something. Ive found it works really well.

it('When update is called, delete is called with the correct parameters', function(){
  var inputCheckbox = '<input type="checkbox" class="delete" id="bc87d270-95bc-49bc-9cac-3e903d09590b" status="Pending">',
      inputCheckboxId = "#bc87d270-95bc-49bc-9cac-3e903d09590b";
  $("body").append(inputCheckbox);

  var spyDelete = spyOn(vm.dataService, "delete").andCallFake(getData);
  var spyGet = spyOn(vm.dataService, "get").andCallFake(getData);

  vm.update();

  expect(spyDelete).toHaveBeenCalledWith("bc87d270-95bc-49bc-9cac-3e903d09590b");
  expect(spyGet).toHaveBeenCalled();

  $(inputCheckboxId).remove();
});

Note: The viewmodel vm is injected into the test using require.js and the function dataService.delete returns a jquery promise

Saturday, 27 July 2013

Monday, 17 June 2013

Life, Death and Education. A couple of inspiring/thought provoking podcasts.

I don't only listen to technical podcasts. In fact there are 3 podcasts in particular that i listen to a lot; Radiolab, LES (London school of economics), and freakonomics radio. I usually post technical things on my blog but this time i thought id blog about a couple of episodes that i have listened to lately, thought provoking, inspirational, amazing, emotional.

How doctors want to die (hint: its very different from the general public)

Radiolab: The bitter end - http://www.radiolab.org/blogs/radiolab-blog/2013/jan/15/bitter-end/
Thought provoking. A difficult subject, death, but one that i think that everyone should talk about. This is a really good episode about how doctors would like to die, and contrasting that with the general public and the expectations that the 2 groups have. i would encourage everyone to listen to this and to get their loved ones to as well, its great for sparking that difficult conversation.
Extract: We turn to doctors to save our lives -- to heal us, repair us, and keep us healthy. But when it comes to the critical question of what to do when death is at hand, there seems to be a gap between what we want doctors to do for us, and what doctors want done for themselves.

Khan Academy - Reimagining Education

LES podcasts: Khan Academy - Reimagining Education - http://www2.lse.ac.uk/publicEvents/events/2013/04/20130410t1830vOT.aspx
Inspirational. Remember school? You either loved it or hated it. This podcast contains a great discussion on education of the future, some really good thinking on mastery and how education could be better in the future. I've got a little girl now and so we are thinking about her education, i hope the schools she goes to are as forward looking as these guys. I agree 100 % that the progression to mastery is definitely the way rather than the old skool approach of a set pace for all.
Extract: Salman Khan tells the inspiring story of how the Khan Academy came to be and shares his thoughts on what education could (should?) be like in the future.

When does life begin? when does it end?

Radiolab: 23 weeks 6 days - http://www.radiolab.org/2013/apr/30/
Emotional. A difficult podcast to listen to as it not only talks about life and death but relates it back to real people and in particular the story of one family, and one very premature baby. As I've said I've got a little girl now and so I related to this story in a big way. It did make me cry! very moving. But it does have a happy ending.
Extract: When Kelley Benham and her husband Tom French finally got pregnant, after many attempts and a good deal of technological help, everything was perfect. Until it wasn't. Their story raises questions that, until recently, no parent had to face… and that are still nearly impossible to answer.

Friday, 7 June 2013

Quick multi cursor support in sublime text 2

In a previous post i listed out all the podcasts i listen to. Whilst creating that list i had to do a little bit of text manipulation, i used some of the powerful multi cursor support from sublime text to do it.
I wanted all my links to be hyperlinks instead of just text.

Below is the screen cast of how i went from a long list of web addresses:
<h2>.net</h2>
http://www.hm.com/
http://www.dnr.com/
http://c9.msdn.com/
...

to a long list of hyper links:
<h2>.net</h2>
<a href="http://www.hm.com/">http://www.hm.com/</a>
<a href="http://www.dnr.com/">http://www.dnr.com/</a>
<a href="http://c9.msdn.com/">http://c9.msdn.com/</a>
...

The process

First do an incremental find (Ctrl+i)
Then select all matched (Alt+Enter)
Copy all selections into clipboard (Ctrl+c)
End
Type the end tag
Home Type the start tag
Paste the links again
Close the start tag

Sublime text 2, get to know it.

Sublime is really powerful, this is just a really simple demo of a simple real world use. ive used it to process hundreds of thousands of lines pulled from databases whilst selecting complex regular expression matches then using sort lines and unique lines to get a really good feel of the data.

I used APowersoft screen recorder to record the screen cast and Key Jedi to display the key strokes as i went.

Thursday, 6 June 2013

My Collection Of Podcasts For Developers

[update "2015-11-03"]I've just posted a new version of this list to developer-podcasts-v2[update]

I'm always on the lookout for more development focused podcasts and thought id better share my current crop as i know others are also looking for more podcasts to accompany them on their daily commute.

My interests

Some background on me: Primarily a .net developer with an interest in agile, javascript, ruby, design and devops. So this list derives from these interests.

.Net

http://www.hanselminutes.com/
http://www.dotnetrocks.com/
http://deepfriedbytes.com/
http://channel9.msdn.com/

General Development

http://www.se-radio.net/
http://herdingcode.com/
http://techcast.chariotsolutions.com/
http://devnology.nl/en/podcast
http://thisdeveloperslife.com/

Javascript

http://nodeup.com/

Ruby

http://ruby5.envylabs.com/

DevOps and IT

http://www.runasradio.com/
http://foodfight.libsyn.com/
http://devopscafe.org/

Business

http://www.startupsfortherestofus.com/

Design and Development

I found this resource last month that contains lots of other great podcasts on design and development http://www.smashingmagazine.com/2013/04/19/podcasts-for-designers-developers/?utm_source=feedly


Friday, 24 May 2013

OO sessions. Learning from failed interviews.

OO Sessions

Earlier this week i did a session with a candidate looking for a job with ThoughtWorks. It was a little strange as she had already failed our coding test, but she was otherwise a really strong candidate. RM asked if she would like to have a session with a couple of us here so we could point her in the right direction (coding wise) and then she may re-apply in six months or so.

She had 3 years experience in .net, mainly writing windows forms applications, and in her eyes had produced some good object orientated code for her submission.

Coding challenges

We (ThoughtWorks) give candidates coding challenges to complete before interview so that we can more accurately asses programming skill. From the spec she had pulled out a good domain model for the problem consisting of 4 or 5 classes which reflected the simple domain quite well. There were attributes on these classes to represent the fields and collections, and also this was all quite well tested. But... this was very much a microsoft approach.

View logic, code behind

All the logic lived in the view of the windows form app. the code behind class had responsibility for everything, from reading files to processing, validation, sorting, and output formatting. None of the methods on this class were tested, in fact none were testable as everything depended on everything else, you had to spin up the app to run it, maybe thats why there were no tests.

Learnings

What struck me though was how intelligent the candidate was. It was great to see that after one and a half hours of discussing her code submission how she now views OOP. How behavior is part of the domain, how utility classes should be part of the domain, and how to unit test things properly. It was great to know we had imparted knowledge on her and that she was excited by what she had learned.

I place a lot of the blame on both Microsoft (for producing simplistic code examples for simple problems, which people then copy) but more so her peers, senior technical folk and technical leaders. These are the people you learn your craft from, these people should be able to develop better solutions than the simple code samples you see all too often in online tutorials. Had they mentored her in a better way she would not just be finding out how to do this stuff after 3 years.

I do hope she does reapply, i liked her and agree with RM that she has lots to offer, just learn a little more OO coding first. Its great that we (TW) are given the opportunity to teach like this. She was not joining the company. She had already failed the interview process. She may join in the future but thats not the point, the point was we did this for free, giving something back, it was great. And I hope everyone i meet who lacks a bit of knowledge or skills is as open as her to different ideas and different ways of doing things. I hope i am.

Monday, 13 May 2013

My experiences with Mac OS - Developer mono-culture

Mac love

I talk to a lot of developers and read a lot of blogs and as i sit here in the office i see the vast amount of developers use Macs. I just got up and looked around 28 Macs, 7 Dells. Everyone raves about Mac, Mac Mac.

Me and my Mac

I used Mac for 2 years, pretty much the entire of 2010 and 2011, I've now had a Dell from 2012 till now 2013.

I've been a long time windows user, with bouts of time using Ubuntu and more recently Mint three and a half years ago i got the opportunity to get a Mac as my company laptop, I've never used a Mac before, so i got one, and hated it, yes I'm a dev... it was the best decision to try it though.

It was good that i was forced to use it at home for two years (it was my main home machine) i did gave it a good chance, until work wanted to replace it when i got the option of a dell or another Mac, i chose Dell. There was a degree of incredulity around that decision, mainly from fellow devs.

Whats up with me?

Whilst i agree the hardware itself was very nice, very well made (although the hard disk did pack in on the Mac after 2 months (nothings perfect)). I never fell for the OS though, far too many little annoyances, i never got on with it. Little things like the finder, interacting with the file system, the way the windows were managed, the lack of a tool bar that you can see your open windows, the way hidden windows can be hard to get back. Im sure people will say these things are non issues and you can do this and that to sort them out but to me it just got in the way of being productive. Then there is windows on bootcamp, yes you can do it but its a very sub optimal experience. from speed issues to keymaps it wasn't great. I do lots of .net often with SQLServer, its just too slow. So you boot into windows which is better, but then the keymaps are a pain. You might was well have a windows machine if you are doing windows development since you aren't using OSX.

So why the best decision for me to use Mac? well every one always raved about mac but never having used one much i was wondering what i was missing. Now I've used one for two years I've been happy going back to Microsoft with mint VM when i need it in a nix OS.

Back to Windows

So im now on windows 8, its a dell latitude, core i7, 8 Gig and find it a great experience. I'm into node and ruby and C# all of which run quick. Whilst now and then things don't work its really few and far between that they don't. The most recent thing was integrating cucumber with phantomJS, they don't play well together on windows at the moment.

Developer mono-culture

I just wanted to get another point of view out there. Sometimes i feel outnumbered as a windows user in certain circles, and it can be hard to speak out against Mac or anything else that developers are supposed to love, i don't think mono-culture is healthy, whether that is operating systems, IDEs, or habits. And I think Microsoft has come a very long way of late. Both communities should interact more there, less of the put downs. We are all writing code in the end its personal preferences in the end.

Applications I use regularly

  • dexpot, nexis file, conemu, launchy, beyond compare
  • visual studio 2012, webstorm, rubymine, sublime text
  • chrome, firefox
  • word, excel
  • SQLServer, MongoDB

Friday, 10 May 2013

A SPA seed - Javascript stack with node and angular.

Single Page Application built on node.js and angularJS


I've been looking at creating a SPA with a full javascript stack so decided to pull together a seed based on node and angular with jshint to test all the .js files, mocha to run the node tests, karma to run the browser based angular tests and cucumber for BDD (full stack testing/acceptance tests).

I did this because i could not find any examples of how to pull together angular and node in the same project along with testing of everything. This is a good start but until i use it in anger i wont really know if ive got it right, so when i do i will try and update it.

https://github.com/DamianStanger/NodejsAngularSPASeed

Details

Node

node.js, npm, angular, karma, Mocha, phantom.js, jshint, jshintRunner

Ruby (1.9.2)

Ruby is for cucumber that is used for the full stack acceptance testing
ruby 1.9.2, devKit, bundler, cucumber, capybara

Next

The readme.md file gives details on how to get it all running and get the tests working. Then just clone this repo and use it as a starting point for your next node SPA app.

Wednesday, 8 May 2013

Developing node modules, npm and git. how to publish npm packages from a windows machine whilst preserving the unix style line endings

I recently had a problem whilst publishing a new node.js module id written that is designed to run jshint recursively against a number of directories and or files.

I develop on windows and so the line endings are dos based (crlf) but the file to run your app as stored in the bin folder needs to have unix line endings (lf) for it to run on a mac or nix systems.

i save code in git and github with unix line endings turned on in the repository but on my working directory the file system is dos line endings. So when i publish using 'npm publish' the file in my bin is published with dos line endings.

this means that when you do an npm install -g on a mac or linux you get the error
env: node\r: No such file or directory

To fix this i developed a little batch script that i use to do the publishing of new versions its really simple and will change the line endings just before publishing.

dos2unix --d2u bin\jshintRunner
npm publish


it uses the built in dos2unix (im running windows 8 ultimate) to change the line endings. I hope this helps someone else out with a similar issue.

Link to the project on github : https://github.com/DamianStanger/jshintRunner and on npm https://npmjs.org/package/jshintrunner

Friday, 26 April 2013

A node application to count lines within a files

Node with Mocha, Should and Sinon to count file lines

I've recently had the requirement to count lines of source code in 3 or 4 different code bases, including a couple of single page web apps written in javascript, angularjs and karma, a couple of java server side services and an acceptance test suite again written in java and selenium.

I wanted to compare the code bases and to look into the ratios of test code to production code so we as a team could get some collective feel for the entire code base, which to me was a very valuable exercise.
I decided to write a command line app in node and javascript, mainly because at the moment I’m trying to boost my javascript knowledge and I’m really interested in node. This command line app would count the lines of code in a code base. There is nothing better than a real requirement to spur you into action.
You can find the source code here: https://github.com/DamianStanger/lineCounter
I’m quite pleased how it has turned out but as is the way with every piece of software I’ve run out of budget (free time) before completion. But I would have liked to enhance it further if I could find the time.

Enhancements:


  • Return a json string that has file and line counts for every directory in the codebase. This output could then be pushed into a d3 app to visualise the source code and the relative sizes, that would be cool.
  • Ability to customise the ignored files and directories.
  • Ability to hook into team city, this would need some new output reporter creating so we could track the lines of code over time.

Learnings

  • I started off using Karma and jasmine for running the tests but found that they were difficult to get to play well with the node modules I created so I switched to Mocha (http://visionmedia.github.io/mocha/) glad I did, because I love it. I especially like the BDD style tests I can write with many nested describes to get the test context. I’m not sure how I’m going to cope going back to the flat structure of nUnit.
  • I started to use Should (https://npmjs.org/package/should) as the preferred mechanism for asserting. The fluent interface is really appealing, it’s very similar to one I’ve been using in .net for a while now.
  • I’ve needed to do a bit of mocking in this project and for this I found Sinon (http://sinonjs.org/ ). Very powerful and flexible, its been capable of meeting all my stubbing and mocking needs up to now. Bit of a learning curve but its all good.