Rosher Consulting

Software Consulting and Development Services

Unit Testing AngularJS with Jasmine, Chutzpah and Visual Studio

Recently I’ve been working on a project that heavily utilised AngularJS for its client side functionality. Since all of my server side C# code was all unit tested I wanted to add some unit tests to my Angular controllers and services. Unit testing C# and server side code is well understood, with plenty of tools available and support baked in to Visual Studio, but the idea of unit testing JavaScript is reasonably new, which means that the number of tools available to a developer (especially a .NET/C# dev) is quite limited and the IDE support minimal.

Fortunately the community has been very active in this area, so everything was available for me to get unit tests added to my project, I just needed to work out how to do it.

Since I’m working with Angular, the first thing I did was look at the code samples on the Angular site, which all come with unit tests, these use the Jasmine testing framework and I came across a couple of good articles on testing Angular controllers and services using Jasmine here and here – Note that I won’t be providing example unit tests with this blog post, the examples on the Angular site and the preceding two links cover it far better than I ever could.

This all seemed straightforward so far, however I wanted to get this nicely integrated into Visual Studio, so a bit more Googling and I came across Chutzpah (pronounced hutz-pah). Chutzpah is an open source test runner which lets you integrate JavaScript unit tests into Visual Studio and enables you to run them from the command line, which means you can integrate your tests into your build process.

Installing Chutzpah

Chutzpah can be installed from NuGet, just right-click on your project or solution in Visual Studio and choose ‘Manage NuGet' packages’, then search for ‘Chutzpah’, it should be the first result found:

chutzpah-nuget

To run Chutzpah from within Visual Studio you will also need to install two plugins, the first is ‘Chutzpah.VS2012.vsix’, this integrates into the unit test explorer window in Visual Studio, the second ‘chutzpah.visualstudio.vsix’ integrates into Visual Studio’s context menu so you can right-click on a test and run it.

Resharper

If you’ve got Resharper installed then it automatically supports running Jasmine unit tests so you won’t have to install the two Chutzpah plugins above. The one thing you’ll probably want to change however is how the tests are run, by default it will run them in the local browser, but you can change this to use the PhantomJS headless browser, which is installed with Chutzpah. Just go to Resharper->Options, then Tools->Unit Testing->JavaScript Tests and change the ‘Run Tests With’ setting to ‘PhantomJS’, then browse to the PhantomJS exe, which is in the Chutzpah NuGet package folder, so mine is set to ‘C:\myproject\packages\Chutzpah.2.5.0\tools\phantomjs.exe’ as below:

resharper-phantomjs

Where to put your unit tests?

There are a number of posts on Stack Overflow (see here, here and here for example) about where to put your JavaScript unit tests i.e. should they go in the same project that is under test or should they be in a separate project? Personally I like to keep my tests separate from the actual code and all of my C# unit tests are in a separate project in my solution file, so I’ve done the same for my JavaScript unit tests.

All you need to do is add a new ‘Class Library’ project to your solution and then add the Jasmine NuGet package to that project.

Running your tests

After installing Chutzpah and creating my first set of tests, I then attempted to run them, but no matter what I couldn’t get my tests to pass. After taking a step back and writing some very simple tests with no dependencies it turned out that I just need to make sure I’d included all of the necessary references in my unit test files.

Chutzpah runs all tests in a single file in isolation i.e. think of it like each of your C# unit tests which need to include any references with ‘using’ statements, so each unit test file has to reference any supporting JavaScript files it needs. References look like the following and should be placed at the top of your test file:

/// <reference path="../../../../myproject/scripts/libs/angular.js" /> 
These are file path references, relative from the tests project to the actual file location, the easiest way to create these references is to simply click and drag the file from the solution explorer into the JavaScript test file and Visual Studio will automatically create the reference for you.

In my tests I was including the references for Angular, Angular Mocks etc., but I hadn’t taken into account any other file references I may have needed, especially as they weren’t being used by the controller/service under test. It turns out that in my ‘App.js’ file I had some JQuery code running on start-up prior to my Angular app being created, which meant that in my tests I had to reference some extra files otherwise my Angular app module couldn’t be created and hence my tests would fail, which wasn’t entirely obvious from the test results (and of course I realise now that including non-Angular code in an Angular file was a bad idea!).

Here’s the list of references I ended up having to include, which as you can see is quite a lot for what was a simple unit test:

/// <reference path="../../../../myproject/scripts/libs/angular.js" /> 
/// <reference path="../../../scripts/angular-mocks.js" /> 
/// <reference path="../../../../myproject/scripts/libs/ui-bootstrap.js" /> 
/// <reference path="../../../../myproject/scripts/libs/sanitize.js" /> 
/// <reference path="../../../../myproject/scripts/libs/jquery-1.9.1.js" /> 
/// <reference path="../../../../myproject/scripts/libs/bootstrap-datetimepicker.js" /> 
/// <reference path="../../../../myproject/scripts/libs/jquery.validate.js" /> 
/// <reference path="../../../../myproject/scripts/app/app.js" />

Once I’d included the above references, my tests all ran fine as you can see in the following screenshot from the Resharper unit test session window:

resharper-js-test-run

 

Integrating your tests into your build

Chutzpah comes with a console application, which means that you can run your JavaScript unit tests from the command line, all you have to do is point it to the folder where your tests are, like so:

chutzpah.console.exe C:\MyProject\MyJavaScriptTests\

One issue I found is that Chutzpah will look for all JavaScript files in that folder and sub-folders, meaning that since we’ve installed Jasmine into our project, Chutzpah will also attempt to load and run those files, which we don’t want. Unfortunately Chutzpah doesn’t support filtering or wildcards in the folder path you specify, so the way I got around this was to place all of my tests into a subfolder and then adjust the command line to point to this folder instead, bypassing any files I didn’t want tested:

chutzpah.console.exe C:\MyProject\MyJavaScriptTests\Tests\

chutzpah-console

Adding Chutzpah to your Msbuild project

Since Chutzpah can be run from the command line, adding it to your build project is straightforward, all you need to do is create an ‘Exec’ task that executes the console application with the appropriate path to your JavaScript unit tests:

<Target Name="RunJSUnitTests">
  <Exec Command="&quot;$(SolutionFolder)\packages\Chutzpah.2.5.0\tools\chutzpah.console.exe&quot;
 $(SolutionFolder)\Tests.JavaScript\Tests\ /silent
 /junit $(MSBuildProjectDirectory)\chutzpah-output.xml" 
WorkingDirectory="$(SolutionFolder)\packages\Chutzpah.2.5.0\tools\" />

  <Message Text="$(TASK_BREAK)" />
</Target>

Here’s the result of running the above build task on my system, if any of the JavaScript tests fail, Chutzpah will return an error and the build will fail:

chutzpah-msbuild

Team City

Chutzpah automatically detects when it’s being run inside Team City and will change its output to the format understood by Team City, so as long as Chutzpah has been added to your Msbuild project, you don’t need to do anything to get support inside Team City as you can see from the following build log:

team-city

Summary

As you can see, there really aren’t that many steps involved in getting JavaScript tests setup in both Visual Studio and your build process, really the hardest thing for me was figuring out why my tests were failing and that was merely making sure my references were setup correctly, other than that everything was surprisingly straightforward, certainly more so than I was expecting when I started on this journey.

House Network Part 1

I’ve had a number of posts planned for a while on the new home network, as it’s been nearly 2 years since we moved house, it’s about time I got around to blogging about it!

Our old house had a home network but it was organic in nature, it started off with a wireless router and was then augmented with a mixture of cat5e and cat6 cables from the router to various rooms as needed. I also had a couple of small Netgear swtiches to help extend it even further, so basically it wasn’t very efficient but it got the job done.

I’d seen a few home network installs on various forums over the years (in particular this), so when we decided to move house, I started to plan what I wanted and as soon as we were settled in the new house I ordered everything I would need to get started with the install.

Toys!48 port patch panel and patch leads

Yes, that is 610m of Cat6 cable! :-)

The loft in the new house is quite big, so it seemed the perfect location for node zero, the only problem was how to run the cables from the loft to the rest of the house? After some debate with the other half we decided that running the cable through our bedroom and in to the garage below was the best option as that also enabled me to run the cables from the garage in to the cupboard under the stairs and then from there under the floorboards, eventually I would box in the cables running through our bedroom (2yrs later and this still isn’t done, whoops).

Lots of cable!Eeek!That's a bit better

Once I’d got to this point, I asked a friend to help me route them downstairs and under the floorboards to the various points in the lounge, it took us a whole day but me managed to get it all done and we’d just put the last floorboard back when the other half came home, phew!

In total the initial run included 11 cables, with 8 points going behind the TV, 2 at the far end of the room and 1 in the hallway by the telephone point.

Through the garageUnder the stairs and floorboardOut they pop

Finishing offFar end of the roomHallway

Initially I wasn’t planning on getting a server rack as I thought they were too expensive, however I found a seller on eBay that were selling them for a great price. In total I bought a 12U rack and 2 x 3U shelves for £103 and I have to say that the quality of the rack is excellent and the packaging was first class, can’t recommend highly enough.

The rack arrivesThe rack installed

End of Part 1 – in Part 2 we’ll expand the system with even more cable runs and I’ll explain what the various points are being used for.

Creating custom recorded TV views in SageTV

SageTV version 7 added the ability to customise the existing 4 recordings views as well as configure up to 4 additional views, while this is covered in the SageTV manual (page 41), it’s not immediately obvious how powerful this feature really is.

Before we discuss customising the recordings views though, we’re going to create some custom user categories, which is also a new feature in version 7, so firstly navigate to your favourites: TV –> Schedule recordings –> Manage favourites, choose one of the favourites to edit and then navigate to the 'User defined categories' column and select ok.

Set User CategoriesSelected user category

From this screen, you can add a new category, you just need to give it a name that’s useful to you, in the screenshot above I’ve already created 3 user categories: Helen, Kids and Paul. Once you’ve created a user category you can assign it to a favourite (in the screenshot above the category ‘Helen’ is assigned to the favourite ‘The Vampire Diaries’), when you’ve assigned categories to the selected favourite, just select 'Done' and from now on all recordings of the favourite will be assigned your chosen user category. If you wish, you can then edit your other favourites and assign some custom user categories to those as well.

You can also assign user categories to existing recordings, just navigate to the recording you want to update, bring up its options menu and choose the 'Edit user categories' option as in the screenshot below, you'll then be asked if you want to edit the categories for the current recording or the favourite, in this case, choose recording and you'll be presented with the same 'Set user categories' screen that you've seen before.

Editing user categories on an existing recording

Now that we’ve configured the user categories, it’s time to start customising the recordings views, so navigate to one of your existing recordings views, such as ‘All recordings’ and bring up the options menu, either using the options button on the remote or by pressing Ctrl-O if using the keyboard. When the options menu appears, choose the 'Menu options' item, this will bring up the following menu:

Updating the number of views

On this menu is an option called 'Number of Views', by default this will be set to 4, go ahead and change this to a number greater than 4, but no more than 8 (in the screenshot, I've already got mine set to 8). Once done, close the menu and then bring up the options menu again, this time as well as the 4 default recording views, you will have 4 additional ones labelled 'Recording View 5' to 'Recording View 8' (again, I've already configured mine so the screenshot isn't exactly what you'll see).

Switching to your new view

Go ahead and select one of the new recordings views, if you’re currently in the 'All Recordings' view then it's unlikely you'll see any changes, except maybe the sorting of the recordings. Now this view is active, you can start to customise it, with the first thing you'll probably want to do is to rename it, which you can do by choosing the 'Menu options' item from the options menu. This will bring up the same menu we saw earlier when increasing the number of views. From here, you can select the menu item 'Rename the ‘Recording View X’ view' and then enter a meaningful name for your view, such as 'Kids Shows'.

Now that you've got a custom view, it's time to start filtering what's displayed in the view, so bring up the options menu again and choose the 'Filtering' item, which will present you with the screen below left (note: all of these screenshots are with the Diamond UI Mod installed, which presents a few more options than the standard UI, so don't worry if things aren't exactly the same). At the moment we're interested in the categories, so choose that option and you'll be presented with the screen below right, showing all of the categories available as well as all of the user categories you created earlier.

Filtering optionsFiltering categories

For this example I'm going to choose the 'Kids' user category and then back out of these screens, choosing 'Done' and then 'Close' to bring me back to my custom view, which should have updated itself to only display recordings that are in the 'Kids' category as per the screenshot below.

The filtered view

I could then go on to further customise this view by filtering out watched recordings for instance or changing the sort order to show the latest recordings first. It's worth noting as well that each view will maintain it's own settings, so setting the filtering and sorting options for one view will not affect any of the other views, which is extremely handy if individuals in your family prefer different sort orders etc.

This is really just a taster of what can be done, but hopefully, with the above information you're beginning to realise just how powerful the user categories and custom views can be. In my setup I’ve now got views setup specifically for me, my partner and one for the kids, which means I'm no longer getting nagged about where the better half's recordings are ;-)

Why I chose not to use SageTV

Visit SageTV.comDamn, I started writing this post over a year ago, much has changed since then, not least the fact that I’m actually in the process of switching to SageTV, whereas this time last year I had no intention of doing so.

I still think it’s worthwhile discussing why I chose not to use SageTV in the first place before moving on to why I’m now switching over.

SageTV Version 6 Main Menu The main reason I have never used SageTV as my media center of choice is because of the UI. In my eyes it’s always been too busy and has never had the same kind of polish as Windows Media Center, Media Portal or XBMC et al. The subject of Sage’s UI has come up in countless topics in the support forums, with many polarised views, some people love it, some hate it, some put up with it, the one thing that most users seem to be able to agree on however is that good or bad UI Sage is still a great media center.

 

SaveTV Version 7 Main MenuIn my opinion the problems with Sage’s UI is the one major stumbling block that’s stopping Sage from elevating itself from being *just* a great media center into a superb media center. In fact, Sage themselves must feel the same as version 7, which has recently been released, has a re-designed UI, albeit in my eyes it looks more like a coat of paint than a complete re-design. The main menu has been given a facelift and is now very similar to many of the skins you see for XBMC, however drill down into the various sections and you begin to see that it still has that same busy looking feel that the last version did.

While Sage may not be kings of the UI, the one thing they have always done well is the recording and playing back of content. Out of all the media center apps I’ve ever used Sage has always offered the most rock solid TV recording and playback experience (MediaPortal has been rock solid recording for the last year or so but playback can still occasionally be flaky), if that’s your main priority in selecting a media center then Sage is the perfect choice.

What is probably my most favourite feature of Sage though is its server/client architecture. Windows Media Center users have been asking for PC based extenders (aka SoftSled) for years and Microsoft have never delivered, Sage users on the other hand have been enjoying PC clients for a long time, not only that but Sage also has a client they call the Placeshifter, this is effectively the same as a PC based client (i.e. it’s software that runs on your PC) but you use it outside of your home network. For example, say you were stuck at work and didn’t want to miss [insert sporting event of choice here], you could use the Placeshifter client to connect to your Sage system at home and watch Live TV, no need to miss a thing. The Placeshifter will automatically adjust the bit-rate of the content your viewing so that it will stream nicely over your broadband connection, which is good in that you don’t have to have a super fast connection to use it but of course you will be sacrificing some quality.

A couple of years ago Sage also introduced hardware based extenders in the form of the HD100, these are low cost, low power devices that can either run standalone as a network media player or they can connect to a Sage server and act as another client. The latest of these extenders has just been released, called the HD300, it’s the 3rd hardware extender from Sage and now supports bit-streaming of HD audio which was one of the last things lacking from Sage’s extenders.

Why I’ve decided to switch to SageTV?

tmp108DIt just so happens to be that it’s this little box that has made me re-consider whether I use SageTV or not. Priced at $150, by the time you account for delivery and customs charges in the UK, it works out to be roughly £150, which is very good value when you compare it to the alternative of say an Atom/ION based PC. (You might want to also factor in the cost of a UK power supply as the HD300 comes with a US 2 prong plug, you can use a shaving adaptor just fine, however I picked up on of these from CPC).

What got me most excited with this extender though is bit-streaming of HD audio. I have bit-streaming working on my HTPC using an ATI HD5450 card, but it was one hell of a process to get to that point, so I was most intrigued at how plug-and-play the HD300 would be. I placed an order for the HD300 and SageTV server as a combined bundle so that I could try them both out.

I’d read reviews of the HD300 saying that it was smaller than the previous extender, what I wasn’t expecting however was just how small this device is. For what it does, it is tiny, I would say that it’s not much bigger than one of my hands and is perfectly suited to being attached to the back of a TV as you would never know that it was there.

Setting up the HD300 was very simple, plug in the various connections, switch it on and follow the on-screen prompts to choose your video and audio connections, you can then choose to run it in standalone mode or connect to a SageTV server, I chose the latter and was presented with the SageTV main menu. I navigated to my videos and proceeded to throw various Blu-Rays rips at it, making sure I tried various video and audio codecs (VC-1, H.264, DTS HD, Dolby TrueHD) which all played perfectly smoothly and my amp reported that it was correctly receiving the bit-streamed HD audio. Score a win for SageTV.

I then tried playing some DVD rips, unfortunately this is where things didn’t quite go to plan, after it breezed through the Blu-Ray’s I was expecting the DVDs to be a formality, but upon watching the opening sequence of Back to the Future I noticed that there was the occasional stutter, I decided to try another film and fired up Star Wars EpII and was horrified at how blocky the picture looked. It was at this moment I had a vague recollection of the Missing Remote reviewof the HD300 in which they mention that the HD300 isn’t particularly strong when it comes to upscaling content, hence why DVD rips don’t look so good. Since my initial trials, Sage have released a number of firmware updates for the HD300 and while I won’t say that DVDs look great, they do appear to be acceptable now and certainly the stuttering that I witnessed is no longer there.

Another issue I initially had with the HD300 was that the UI looked blurry on my TV, I eventually learned that the reason was because the extenders render the UI at a lower resolution and then upscale it to the output resolution, hence the blurriness. To me, this was so bad that I’d decided the HD300 wasn’t for me and I’d boxed it up ready to move it on but kudos to Sage, they released an update for the HD300 that bumped the UI up to 720p and once I applied the new firmware the difference in the UI was substantial.

One of SageTV’s greatest strengths are the community plugin developers who have produced a number of fantastic additions that have really raised SageTV to new heights. With the addition of the new plugin manager in version 7, installing and trying these various plugins is now easier than ever, which means that I’ve been trying quite a few to see how they fare. The following is a (short) list of my recommended plugins:

  • Metadata Tools (otherwise known as BMT/Batch Metadata Tools) – Provides automatic metadata and fanart retrieval for your SageTV recordings, ripped DVDs, Blu-Rays, TV Shows etc. which can then be used by UI plugins.
  • SageTV Web Interface – Provides a web UI that allows you to manage and schedule recordings, watch your recordings, control your clients and much, much more.
  • SageTV Mobile Web Interface – As above but for mobile devices.
  • MiniGuide – Provides a mini guide while watching live TV allowing you to see what’s currently on other channels and switch over without having to bring up the full guide.
  • Diamond Theme – This theme provides more than just a facelift for Sage, integrating the metadata and fanart that BMT retrieves and proving new views of your media (see screenshots below).
  • SageMyMovies – Provides integration of MyMovies in to SageTV. 

Here’s a few screenshots showing by current setup, with the above plugins installed:

Home Screen - Diamond ThemeImported TV - Diamond Theme Custom ViewTV Seasons View - Diamond ThemeVideos by FolderMy Movies - Sideways ViewMusic by Artist - Customised with 3 columns, reduced font size etc.TV Guide - Channel Icons from iconharmony.comMini Guide

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

This post started off with the intention of telling you why I don’t use SageTV, however in the course of a year much has changed which is a testament to the continuing efforts of the SageTV team who have really raised the bar and come good with a fantastic new version of SageTV, let’s just hope that trend continues over the next year.

To finish off, here’s a short summary giving some pros and cons to using SageTV:

Why you would want to use SageTV:

  • Rock solid TV recording and watching
  • Integration with Windows Home Server
  • True Server/Client architecture – use a PC as an extender or a dedicated SageTV Extender device
  • Dedicated hardware extenders that support 1080p and Bitstreaming
  • Placeshifting – watch Live TV (and all your content) anywhere you are in the world as if you were still at home
  • Lots of community plugins
  • Cross platform – runs on Windows, Mac and Linux.

Why you wouldn’t want to use SageTV:

  • UI isn’t as polished as alternatives (can really affect the Wife Acceptance Factor)
  • No out of the box support for the MCE IR blaster (I’ve always thought this was a bit strange, the remote is supported but not blasting! A third-party plugin does enable it though, so not a major setback)
  • Media Center aspects (Music, Pictures, Movies etc.) seems to take a back seat compared to TV functions (3rd-party plugins have improved this i.e. SageMyMovies.)
  • No true music visualisations and no support for 3rd party Windows Media Player or WinAmp visualisations, although the new HD300 extender does have a new visualisation, which is better than the stock graphics one you get in the client. While I do miss this, in our house we tend to like watching the pictures screensaver when it comes on anyway, so not a big problem anyway.

Choosing your preferred Media Center Software

Over the next few weeks I intend to post some new articles explaining why I did or did not choose a particular Media Center application, what I think are the good points and bad points and why you should or shouldn't choose one over the other.

The point of these posts isn’t to criticise any of these apps, indeed I think they’re all great in their own way, it’s more a case of fulfilling my own particular needs and hopefully I’ll be able to help others out in the process.

Here’s a list of the posts I’m planning which I’ll update as I post each one:

  • Why I chose not to use SageTV
  • XBMC and its offshoots (Plex, Boxee et al)
  • MediaPortal
  • The enigma that is Windows Media Center
  • Media Center apps that I haven’t already covered

How to send your Windows Home Server to sleep via the Home Server Console

I have my Home server configured to wake up for a couple of hours a day to perform backups and then go back to sleep again, which is made possible via the excellent LightsOut add-in. However I also occasionally use my Home Server outside of these hours by sending it a WakeOnLan (WOL) request to wake up as I can’t physically switch the machine on as it’s up in the loft out of the way.

Once it’s up and running, the question then becomes how do I send it back to sleep again? The most obvious answer is that I can remote desktop into it and send it to sleep there, but that’s a bit long winded especially when you’ve most likely got the Home Server console open anyway.

My solution involved two third party applications, the first is PowerOff which allows you to control the power state of your pc and the second is Advanced Admin console, which is a Home Server add-in. Once setup with these two applications you’ll be able to send your Home Server to sleep simply via a toolbar button.

Here’s the steps I followed to get this functionality:

  1. Download PowerOff and copy it over to your Windows Home Server.
  2. In the same place where you copied the PowerOff executable, create a batch file called ‘PowerOff.bat’.
  3. Edit this file and enter the following (don’t forget to change the folder location to where you copied PowerOff):
    1. D:\shares\Software\poweroff.exe standby –immediate
  4. Save the batch file and close it.
  5. Download Advanced Admin console and copy it to your Windows Home Server add-ins folder.
  6. Start the Windows Home Server console.
  7. Click on ‘Settings’ and then go to the ‘Add-ins’ section.
  8. On the ‘Available’ to install tab, select the Advanced Admin console and install it.
  9. Once the add-in is installed, log back in to the Home Server console.
  10. Switch to the Advanced Admin console add-in.
  11. Click the drop down arrow at the far right of the toolbar and click ‘Customize…’.
  12. Switch to the ‘Custom shortcuts’ tab and click ‘Add Shortcut…’.
  13. Browse to where you created the ‘PowerOff.bat’ file and then click ‘Next’.
  14. Name the shortcut ‘Sleep’ and then click ‘Finish’.image
  15. Click ‘OK’ to exit the settings dialog.
  16. Now when you click the drop down arrow at the far right of the toolbar, you should see your new ‘Sleep’ shortcut.image

If you click on your new shortcut your Home Server will go to sleep. When you choose the command, it may appear as if your Home Server Console has locked up, it hasn’t, the Home Server is just doing its thing and after a while the Console will say it has lost its connection to the Home Server, meaning your Home Server is now asleep!

CropperCapture[2] CropperCapture[3]