Pester and PowerShell: TDD

There is obviously nothing special about Pester that encourages you down the TDD route. But assuming you start out with a test structure pattern that works for you, then with some trivial cosmetics, I find it is pretty good fun to do.

An example of some failing tests:

And following a fix to the code under test…

This is my overarching test runner:

For contriving a TDD approach out of this, you could:

clone my repository from GitHub here

From this location, delete the section below, so that you have a test but no SUT for it

When you execute RunAllTests.ps1, you see this:

You can then create nothing but the function name…

and execute RunAllTests.ps1 again…

, and eventually you get to a point when you have fully working code…

… and all tests passing:


Jasmine: also has a CDN version

Such that you could stick this in a template, and not bother about downloading the artefacts:


June 8 2016 However, there was a bit of a fatal flaw: Jasmine having a CDN is fine… but the Angular Mocks library is harder to find as a CDN artefact. This is the one I found and currently works:

The whole thing…

And how that appears when all the tests fail…

The page as text:

<!DOCTYPE html>
<html lang="en">

        <meta charset="utf-8">
        <title>Jasmine as a UI</title>

        <!-- ensure that all the Jasmine files come before all the Angular files... -->
        <link rel="stylesheet" href=""></link>
        <script type="text/javascript" src=""></script>
        <script type="text/javascript" src=""></script>
        <script type="text/javascript" src=""></script>

        <!-- ensure that you include the core Angular file... -->
        <script src=""></script>

        <!-- before you reference the Angular mocks file -->
        <script src=""></script>

        <!-- include source files here... -->
        <script src="NamesApp.js"></script>

        <!-- include spec files here... -->
        <script src="NamesApp.spec.js"></script>




Getting Karma working

I know I’ve talked about this before, but just some screenshots to get to the starting point for the AngularphoneCat application.

PowerShell: generating test data

# Create a set of fixed width records purely for volume testing purposes. The unique key part
# is generated for each record, and the remainder of the record is repeated every time. In the
# constants below, the $COMPOSER_RECORD_LENGTH is inclusive of $KEY_LENGTH.
# Note that the creation of records using StreamWriter is very much faster than using Out-File:
# at some point I'll get around to measuring that. But for now, 0.5 million records on an I3 with
# SSD takes about 1 minute.

# Edit below here as required...
$OUTPUT_FILE = 'C:\temp\BulkComposers.txt'
$RECORD_REMAINDER = "Mozart Wolfgang Amadeus Leopold Salzburg Vienna 1756 1791 "
# Edit above here as required...

$NEWLINE = "`r`n"

$mode = [System.IO.FileMode]::Create
$access = [System.IO.FileAccess]::Write
$sharing = [System.IO.FileShare]::Read
$encoding = [System.Text.UTF8Encoding]
$bufferSize = 4096

$fs = New-Object System.IO.FileStream($OUTPUT_FILE, $mode, $access, $sharing, $bufferSize, $encoding)
$stream = New-Object System.IO.StreamWriter($fs)

"Starting creation of $($RECORDS_TO_CREATE) composer records"

1..$RECORDS_TO_CREATE | foreach {
 $composerId = [guid]::NewGuid().ToString().PadRight(50)
 $composerRecord = $composerId + $recordRemainderPadded
 $stream.WriteLine($composerRecord, [System.Text.UTF8Encoding])

if ($_%$DEBUG_FREQUENCY -eq 0) {
 "$(get-date): Creating record $_"

"Completed creation of $($RECORDS_TO_CREATE) composer records"
"See file [$OUTPUT_FILE]"


SQLServer: random integers

The Rand() function in SQLServer is next to useless for anything set based


select * into t_1 from sysobjects
select rand() from t_1

I’m not going to add it in, but the addition of a seed helps not a jot.


However… CRYPT_GEN_RANDOM() is your friend (the performance overhead might need review, but  I don’t care for my use-case, which is to generate test data):



More interesting:


That variety gives me everything  I need, and can be also used as the basic for randomizing dates in the past and future using date arithmetic, and floats.

select * into t_1 from sysobjects

select rand(), CRYPT_GEN_RANDOM(8),
abs(convert(int,CRYPT_GEN_RANDOM(4))) from t_1



Visual Studio 2015: SpecFlow

Dunno why, but installing SpecFlow in Package Manager console works… but then I can’t find the items.

So going here, gives you this:


Same how-tos here and page dedicated to 2015 here.

Install it…


Then when you stop and start VS, you get this:


I had already created a MSTest project inside a console application solution. Before I didn’t have the SpecFlow items, now I do, when I go to add an item:


The first time of asking, we get this:



So easy enough to resolve the SpecFlow reference, now just the NUnit.

This is the app.config for the other project (sic):


And I’ve seen comments saying that you need this line (first for context):

      <unitTestProvider name="MsTest.2010" />

I dragged the updated app.config into the Unit Test Project, thus…



Also note that the generated file now contains MSTest references, not NUnit references, since we have the new entry in the app.config (the squiggles are because it can’t find a schema for SpecFlow:


Perhaps now I can get to the error I was trying to demonstrate. Oh but first I have to give it some bindings:


So now we have that (PasteBin here)…


Then the test passes…


And then I was playing with the tags trying to get it to break with this error re ScenarioContext and missing key… but I can’t now (get it to break).