Node.js: The [assert] package

I was tempted to write something about Assert with examples… but no point, as this person/persons has/have done a way better job than I would/could (Ed: enough).

And, in fact, this is part of a very generous GitBook:

[Later…] in fact I gave the book credit for giving more comprehensive coverage of Node.js that I assumed on first glance. This is the sum total of the book’s coverage:
NodeJs05

Regardless, thanks to Nelson/Nelsonic for at least giving us this.

 

Advertisements

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:

JasmineCdn01.PNG


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">

    <head>
        <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="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.css"></link>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine-html.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/boot.js"></script>

        <!-- ensure that you include the core Angular file... -->
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.1/angular.js"></script>

        <!-- before you reference the Angular mocks file -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.1/angular-mocks.js"></script>

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

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

    </head>

    <body>
    </body>

</html>



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...
$RECORDS_TO_CREATE = 500000
$DEBUG_FREQUENCY = 5000
$OUTPUT_FILE = 'C:\temp\BulkComposers.txt'
$KEY_LENGTH = 50
$RECORD_REMAINDER = "Mozart Wolfgang Amadeus Leopold Salzburg Vienna 1756 1791 "
$COMPOSER_RECORD_LENGTH = 1092
# Edit above here as required...

$NEWLINE = "`r`n"
$recordRemainderPadded = $RECORD_REMAINDER.PadRight($($COMPOSER_RECORD_LENGTH-$KEY_LENGTH))

$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]"

$stream.Dispose()
$fs.Dispose()
$stream.Close()
$fs.Close()

SQLServer: random integers

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

PSRandGen04

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.

CRYPT_GEN_RANDOM

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):

Interesting:

PSRandGen05

More interesting:

PSRandGen06.JPG

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(1))),
abs(convert(int,CRYPT_GEN_RANDOM(2))),
abs(convert(int,CRYPT_GEN_RANDOM(3))),
abs(convert(int,CRYPT_GEN_RANDOM(4))) from t_1

PSRandGen07

 

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:

SFlowDownload01

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

Install it…

SFlowDownload02

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

SFlowDownload03

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:

SFlowDownload04

The first time of asking, we get this:

SFlowDownload05

SFlowDownload06

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

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

SFlowDownload07

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

<specFlow>
      <unitTestProvider name="MsTest.2010" />

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

SFlowDownload08

SFlowDownload09

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:

SFlowDownload10

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

SFlowDownload11

So now we have that (PasteBin here)…

SFlowDownload12

Then the test passes…

SFlowDownload13

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).