Archive for the ‘Code and Development’ Category

Sanity check, nykterhetstest

October 6th, 2021

In a (unit) test I sometimes add extra tests to check the test tests what is anticipated.

user = new User(Id="A", Name = "N1")
sut.Create(user)
saved = sut.Get("A")

saved.Name.Should.Be("N1", "Sanity check we know what we have stored.")

// Act.
sut.Clear("A")

// Assert.
result = sut.Get("A")
result.Name.Should.Be(null)

Notice the “sanity check” to verify a bug didn’t set Name to null when creating the user.

My naming convention is “Sanity check” or “Nykterhetskontroll” in Swedish.
[I know the translation, word per word, is incorrect.]

Azure devops tests fail with very little clue

September 9th, 2021

I updated some references in my Azure Devops build and got the following error in the unit testing task:

...
##[error]Error: The process '/opt/hostedtoolcache/dotnet/dotnet' failed with exit code 1 
Result Attachments will be stored in LogStore 
Run Attachments will be stored in LogStore 
No Result Found to Publish '/home/vsts/work/_temp/_fv-az101-233_2021-09-09_10_21_12.trx'. 
No Result Found to Publish '/home/vsts/work/_temp/_fv-az101-233_2021-09-09_10_21_17.trx'. 
No Result Found to Publish '/home/vsts/work/_temp/_fv-az101-233_2021-09-09_10_21_24.trx'. 

##[warning].NET 5 has some compatibility issues with older Nuget versions(<=5.7), so if you are using an older Nuget version(and not dotnet cli) to restore, then the dotnet cli commands (e.g. dotnet build) which rely on such restored packages might fail. To mitigate such error, you can either: (1) - Use dotnet cli to restore, (2) - Use Nuget version 5.8 to restore, (3) - Use global.json using an older sdk version(<=3) to build 
Info: Azure Pipelines hosted agents have been updated and now contain .Net 5.x SDK/Runtime along with the older .Net Core version which are currently lts. Unless you have locked down a SDK version for your project(s), 5.x SDK might be picked up which might have breaking behavior as compared to previous versions. You can learn more about the breaking changes here: https://docs.microsoft.com/en-us/dotnet/core/tools/ and https://docs.microsoft.com/en-us/dotnet/core/compatibility/ . To learn about more such changes and troubleshoot, refer here: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/build/dotnet-core-cli?view=azure-devops#troubleshooting 

##[error]Dotnet command failed with non-zero exit code on the following projects : 
Async Command Start: 
Publish test results Publishing test results to test run '1136330'.
...

Especially the last, “Dotnet command failed … on the following projects” does not give the clue I am looking for. What failed?

To cut a long story short: One of my test project lacked reference to Microsoft.NET.Test.Sdk.

A longer story is that I updated from xunit.runner.visualstudio from 2.4.1 to 2.4.3 and it doesn’t have a reference to the Microsoft.Net.Test.Sdk project as it is no longer Visual studio that holds the reference but the project itself. The clues I got were from https://github.com/xunit/visualstudio.xunit/issues/219 and https://github.com/xunit/visualstudio.xunit/issues/259 in no special order.

Error message “Can’t bind to ‘ngIf’ since it isn’t a known property of ‘div'” solved

June 11th, 2021

When testing a component in Angular you might receive the follwoting warning:

Can’t bind to ‘ngIf’ since it isn’t a known property of ‘div’

It might depend on you not declaring the component-under-test.

See this repo.

describe('AppComponent', () => {
   beforeEach(async () => {
     await TestBed.configureTestingModule({
       imports: [
         RouterTestingModule
       ],
       declarations: [
         AppComponent
       ],
     }).compileComponents();
   });
   it('should create the app', () => {
       const fixture = TestBed.createComponent(AppComponent);
       const app = fixture.componentInstance;
       expect(app).toBeTruthy();
     });
 });

The code is slightly different than the boiler plate Angular/Jasmine test code but still visualises the issue.

The row

const fixture = TestBed.createComponent(AppComponent);

Creates a component that must be declared in

declarations: [
  AppComponent
],

It took me an hour to find it due to code being convoluted.
If it saves you 5 minutes, it was worth writing this post.

When programming we should have 1 based index

December 21st, 2020

It is a bold statement.

Well… is it?

IIRC the most usual, unwanted, exception we get is null reference error.
The second is off-by-one error.

The off-by-one error is the result of us programmers on remembering to subtract one from an index, or subtracting once too many.

Alas we should have 1 based index, like natural language, and let the computer do the subtraction.

What…? That is not how computers work!

No.

But that is how Humans work.

It is many years since we stopped arguing that a computer wants its data layed out such and therefore we adopt to it. Today we try to solve business problems, not computer problems (your domain may vary). We use higher level language. We name our variables after the domain. We use domain language.

We do not ask the customer for the zeroth invoice. And when we ask for the 2nd invoice we expect the 2nd invoice in a list.

The reason for 0 based index is, I believe, a historical technical reason.

When programming assembler, it is easy to point to a memory position and then add an index. Hence having a zero based index is a natural thing; we solve a computer problem.
Then C, a higher level language, comes along. It is way above assembler but is till constructed to be close to the physical machine to be easy to implement on any processor. By that time we still solved computer problems and keeping a zero based index was probably a no brainer.

Then came Java and C#. They try to look like C/C++ due to reasons of not-thinking-things-through and it-is-easier-to-not-move-the-cheese. They keep the zero based index even though they didn’t have to. So now we are stuck with it.

Until either 1) we invent a new operator

1
myArray@1
or
1
myArray/1/
or 2) invent a yet new language.

Until then: Keep spreading the word that we should have a 1 based index.

Write (arrange) Act and Assert in your automatic tests

November 14th, 2020

It is a good custom to write out Act and Assert in test as it helps to write the test in a clean way; and helps the next reader to understand where Arrange stops and Act begins. It helps the writer to not involuntarily write Act statements in Assert.
Like so:

myTest()
sut = setup()

// Act.
sut.do()

// Assert.
assert(sut.value).equals(12)

The above code is too simple to make the upside of this coding standard visible but soon tests get a few lines long and commentning Act and Assert is a good idea.

I don’t bother to write out // Arrange any more as all tests start with Arrange anyway.

Well… that was not true. I have stumbled upon tests with several Arrange, Act and Assert in them. Typically for integration tests.

Tip: fail explicitly during developing a test

August 17th, 2020

When figuring out how to write a test – add an assert that always fails in the bottom. Remove it when you know the test works as it should.

Why: Because you might get interrupted to spend energy on something else. When you get back the test shines red and you know where to continue. No risk of sending sub par test code to a PR.

Starting an Aspnet core site on IISExpress

April 16th, 2020

One cannot point at a folder and just start, as we did with the full framework. There seems to be several solutions for Core and the one described here requires a publish. (Visual studio 2017:menu->Build->Publish)

Publishing a dotnet core site copies relevant parts to a folder

"path\iisexpress.exe" /path:"path\site\bin\Debug\netcoreapp2.2\publish" /port:betweenmagicalnumbers

Here is the exact command I recently used.

"C:\Program Files\IIS Express\iisexpress.exe" /path:"C:\DATA\PROJEKT\Lagardsdorren\Lagardsdorren\bin\Debug\netcoreapp2.2\publish" /port:44342

The port number is limited to 44300 to 44399 to avoid having to start Visual studio with elevated privileges. Reference.

Do not walk the path with “..” as you get an “Invalid physical path.” error.

Set an environment variable to give better debug output. Reference.

$Env:ASPNETCORE_ENVIRONMENT = "Development"

Change background colour in vscode

March 14th, 2020

It should be easy, and is, but the information is hard to find.

Go to Settings (ctrl-,), search for “color customizations”, select “Edit in settings.json”.

A file like this is opened.


1
2
3
4
5
6
7
8
9
10
11
{
    "editor.minimap.enabled": false,
    "editor.fontFamily": "Selfelected1337 Regular", Consolas, "Courier New", monospace,
    "window.zoomLevel": 0,
    "editor.lineNumbers": "off",
    "typescript.updateImportsOnFileMove.enabled": "always",
    "editor.unfoldOnClickAfterEndOfLine": true,
    "editor.columnSelection": false,
    "workbench.colorTheme": "Tomorrow Night Blue",
    "workbench.preferredDarkColorTheme": "Default Light+",
}

(yupp, that’s my own font, Selfelected1337)

Add your setting.


1
2
3
4
5
6
7
8
{
    ...
    "workbench.preferredDarkColorTheme": "Default Light+",

    "workbench.colorCustomizations": {
        "editor.background": "#005ccc"
    }
}

If found the answer github repo which led to official docs.
How to change other colours still eludes me.

Update

I have found documentation for all? colours and now my theme overide is:


1
2
3
4
5
"workbench.colorCustomizations": {
    "editor.background": "#005ccc",
    "sideBar.background": "#0073ff",
    "activityBar.background": "#1a81ff",
},

Update

With later version (20210517) there is a built-in GUI for this. Just press

1
ctrl-,

Cosmosdb change feed and deleted items

October 23rd, 2019

Until Microsoft implements change feed notifications for deletions we have to soft delete items in Cosmosdb if we use change feed notifications.

TL;DR

Cosmosdb has a handy feature where it journals changes which can then be read in another process. My use was for updating an Azure search index (Elastic search variant) whenever I made a change.

But… how do we handle deletions?

Hidden in the documentation is the information that ,,albeit all changes (within a shard) is guaranteed to be in chronological order it is always the last (present) item handed to the change feed process. So when an item is deleted there is no item and, due to implementation, also no event.

Remedy 1: Soft delete the item by marking it as “deleted”. This is a common approach in many a system.
A drawback is that the repository will end up with stale data.

Remedy 2: Soft delete the item as above. Then make sure this information has been conveyed (in my case the item should be deleted from the search database) before properly deleting it.
This is a more complex solution where the logic still has to handle soft deleted items while also adding the complexity of yet a process to scan(?) the repo for items to delete.

Your turn

I have found no uservoice, github or azurefeedback item for voting on getting a delete event; the closest is an item for getting a event for it in Azurefunctions.
In lack of better place I voted there; so can you if you have the need.

Update

I wrote a suggestion of my own here at azurefeedback.
I did try to find an existing suggestion but failed. Am I the only one deleting data? Or the guys at Microsoft will find an already existing suggestion where I can move my votes.

Update

The functionality is on its way
Support for deletes on Change feed is planned. Do not have an exact ETA but we are targeting some time in 2020.

Possibly possible to install Powershell with one line of code

July 27th, 2019

Scott Hanselman wrote about installing Powershell as a global tool here.

It goes something like:

dotnet tool install --global PowerShell

Update

As Windows10 nowadays comes with Powershell this is not so much of a problem any more.