How to unit test Internal classes in .NET Core applications?

Introduction

The creation of unit tests is an important step in ensuring the quality of a project. In general, most public methods are tested, but what if you want to test a non-public part of the project?

Putting all classes of your .Net project in public is not recommended. From the moment when you reference your assembly in a project in Visual Studio, you will have access to all your classes, methods, properties and fields marked as public, and there are surely parts of your code that it is not worth better not leave accessible because they could change the behavior of the assembly or have any negative impact. This is why the keywords “internal” and “private” exist. Only then, your non-public types and members can not be called directly from your unit tests because (in principle) your tests are in separate assemblies.

Fortunately, there is a solution for testing non-public types and members, specifically, internal.

In this article I will show you how to achieve our goals.

We will use an attribute, InternalsVisibleTo, which will make it possible to specify that a particular assembly will have access to the types and members marked as being internal of the current assembly.

Solution

Let’s define an internal class to test like this named “Hello

Then let’s try to test it in a Unit testing project:

As you can see, it doesn’t work the “UnitTest” project can’t see the internal class

Now let’s add [assembly: InternalsVisibleTo(“UnitTests”)] as decorator on the namespace, it should solve our problem now 🙂

 

Beautiful isn’t it? 😉

Streaming video asynchronously in ASP.NET Core 2 with Web API

 

Introduction

No matter what kind of project you are working on, at some point you will find the need to stream videos in your website or mobile apps for many purposes. So, we will learn how we can live stream our video content over HTTP, using ASP.NET Core 2 Web APIs.

Set up a service that get a Stream from a video hosted in the web using HttpClient

Example with videos hosted in Azure:

public class AzureVideoStreamService : IAzureVideoStreamService
{
   private HttpClient _client;

   public AzureVideoStreamService()
   {
      _client = new HttpClient();
   }

   public async Task<Stream> GetVideoByName(string name)
   {
      var urlBlob = string.Empty;
      switch (name)
      {
         case "earth":
         urlBlob = "https://anthonygiretti.blob.core.windows.net/videos/earth.mp4";
         break;
         case "nature1":
         urlBlob = "https://anthonygiretti.blob.core.windows.net/videos/nature1.mp4";
         break;
         case "nature2":
         default:
         urlBlob = "https://anthonygiretti.blob.core.windows.net/videos/nature2.mp4";
         break;
      }
      return await _client.GetStreamAsync(urlBlob);
   }

   ~AzureVideoStreamService()
   {
      if (_client != null)
      _client.Dispose();
   }
}
public interface IAzureVideoStreamService
{
   Task<Stream> GetVideoByName(string name);
}

Then don’t forget to register the service and it’s interface in Injection Dependency system

public void ConfigureServices(IServiceCollection services)
{
   services.AddMvc();
   services.AddScoped<IAzureVideoStreamService, AzureVideoStreamService>();
}

Set up a Streaming controller that stream videos using FileStreamResult object

FileStreamResult sends binary content to the response using a Stream instance

Example of an API that return a video by its name:

[Route("api/[controller]")]
public class StreamingController : Controller
{
   private IAzureVideoStreamService _streamingService;

   public StreamingController(IAzureVideoStreamService streamingService)
   {
      _streamingService = streamingService;
   }

   [HttpGet("{name}")]
   public async Task<FileStreamResult> Get(string name)
   {
      var stream = await _streamingService.GetVideoByName(name);
      return new FileStreamResult(stream, "video/mp4");
   }
}

Run the Web API to test….

 

 

Easy isn’t it ? 😉

How to pass in Uri complex objects without using custom ModelBinders or any serialization?

Do you wonder why I publish such an article?
Well, recently it happened to me to ask to a legacy information system in which I was to find information of users only with their name and their first name…
Yes yes using their name first name:).

I had to send to the WebApi a list of name/first name, the whole paginated, how did I make it?

At the beginning I had the idea to create my search model object in JSON which I would have to serialize while passing it in the URL, then, I had the idea to create an object with a string property containing the list of the names/firstnames the whole “decorated with separators” then split it in the WebAPI with a custom ModelBinder. Finally I decided I’d rather understand how to pass in the Uri more or less complex objects without using of ModelBinder or serialisation?.

Here what I did:

I tested 2 scenarios, the first with a list of “Person” and a second with a little more complex object “ListOfPersons”.
Before starting, as a reminder, I just want to remind you how we pass in a Uri a string collection.

namespace QueryStringDemo.Models
{
    public class Person
    {
        public string FirstName { get; set; }

        public string LastName { get; set; }
    }
}
namespace QueryStringDemo.Models
{
    public class ListOfPersons
    {
        public int? PageNumber { get; set; } 
        public int? PageSize { get; set; }
        public List Persons { get; set; }
    }
}
using QueryStringDemo.Models;
using System.Collections.Generic;
using System.Web.Http;

namespace QueryStringDemo.Controllers
{
    [RoutePrefix("api/demo")]
    public class DemoController : ApiController
    {
        [Route("simplelist")]
        [HttpGet]
        public IHttpActionResult Get([FromUri]List strings)
        {
            return Ok();
        }

        [Route("simplelistOfpersons")]
        [HttpGet]
        public IHttpActionResult Get([FromUri]List listOfPersons)
        {
            return Ok();
        }

        [Route("complexlistOfpersons")]
        [HttpGet]
        public IHttpActionResult Get([FromUri]ListOfPersons listOfPersons)
        {
            return Ok();
        }
    }
}

1- Scenario 1, reminder, string collection :

Uri :

scenario1-url

Result in code :

scenario1-code

Note that you have to use the attribute [FromUri], it provides deserialization to the target object from a complex object that comes from the queryString.

2- Scenario 2, collection of person object :

Uri :

scenario2-url

Result in code :

scenario2-code-1

Note that the object is in fact an array, an array like this

?[index].PropertyName=Value

3- Scenario 3, a more complex collection of person object :

Uri :

scenario3-url

Result in code :

scenario3-code

Note that it works also with an array,
our object looks like this :

?PropertyName1=value&PropertyName2=value&PropertyName3[index].SubPropertyName1=value&PropertyName3[index].SubPropertyName2=value

Finally we have demystified how queryString native serialization/deserialization works, was it so complicated ? 🙂