Friday, February 26, 2021

NUnit–SetUp and TearDown methods

NUnit allows you to bootstrap individual tests through the SetUp and TearDown attributes. I typically try to keep my test classes clean and one way to do this is by moving logic to a test base class.

NUnit will walk through the inheritance tree and call all Setup and TearDown methods. Setup methods (both types) are called on base classes first, then on derived classes. If any setup method throws an exception, no further setups are called.

Something I noticed in the documentation:

Teardown methods (again, both types) are called on derived classes first, then on the base class. The teardown methods at any level in the inheritance hierarchy will be called only if a setup method at the same level was called. The following example is illustrates the difference.

So if you have a test structure like below, only the base SetUp and TearDown are called:

This is a breaking change from NUnit 2.x and something to be aware of…

Thursday, February 25, 2021

MassTransit–How to test consumers with dependencies

I’m using Dependency Injection in my MassTransit consumers. To test these consumers you can use MassTransit in-memory test harness;

But when you try to use the code above with a consumer that has dependencies injected through the constructor, it will not work and you end up with the following error message:

'SubmitOrderConsumer' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'ConsumerTestHarnessExtensions.Consumer<T>(BusTestHarness, string)'

To fix this, you have to change your test code and use the consumer factory method:

Wednesday, February 24, 2021

Elastic App Search - JSON must be an array or hash

I’m a big fan of ElasticSearch. One of the products in the Elastic suite is App Search.

I tried to get some documents through the App Search API using the following code:

Unfortunately this didn’t work but resulted in the following error message:

JSON must be an array or hash

The reason I got this exception is because the API is expecting a JSON array or object for the id parameter.

Unstructured JSON data is not permitted. You have to encapsulate your object in an array [] or a hash {} (what the error message is referring to).

If you are using query parameters, ensure that ids is followed with %5D%5B - this is an array, but escaped: ?ids%5D%5B=:

Tuesday, February 23, 2021

NuGet - Change the location of the global package cache

One of the improvements that were introduced to NuGet a few years ago was the introduction of a global package cache. Instead of having a copy of every nuget package inside your project folder, all nuget packages are downloaded and stored once on a central location on your file system. Each package is fully expanded into a subfolder that matches the package identifier and version number.

By default these packages are stored at the following location:

  • Windows: %userprofile%\.nuget\packages
  • Mac/Linux: ~/.nuget/packages

You can override this by setting the NUGET_PACKAGES environment variable, the globalPackagesFolder or repositoryPath configuration settings (when using PackageReference and packages.config, respectively), or the RestorePackagesPath MSBuild property (MSBuild only).

Remark: The environment variable takes precedence over the configuration setting.

Monday, February 22, 2021

Dapr–Ebook

With the v1 release of Dapr, it is time to investigate it and evaluate how it can help simplify your cloud-native application development.

A good introduction is the following free e-book: Dapr for .NET Developers

Thursday, February 18, 2021

.NET Core HttpClient - Uploading multiple files through a multipart/form-data POST

 Yesterday I blogged about uploading files through Postman. Let's now move on to the real application and implement the client API to upload the files using the .NET Core HttpClient.

The code is not that hard but I'm sharing it here just a reminder for myself:


Wednesday, February 17, 2021

Postman - Uploading multiple files through a multipart/form-data POST

 I had to test an ASP.NET Core API that was expecting multiple files through a multipart-formdata request.

Here are the steps I had to take to get this done through Postman:

  • Open Postman and click on the '+' sign to create a new request:

  • Change the request type to 'POST' and enter the request URL:

  • Click on the 'body' section and change the type to 'form-data':

  • Enter a key name for the files you want to upload(in my case it was 'files'):
  • Now comes the important part, go to the right border of the Key column. A dropdown appears where you can change the type from 'text' to 'file':
  • Now the Values column changes and you get a 'Select Files' button that allows you to specify the files you want to upload:

That should do the trick...