Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit test an internal class in ASP.NET Core

I want to unit test a class with internal protection level in an ASP.NET Core project. I have added an AssemblyInfo.cs file to the properties of the project under test:

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: InternalsVisibleTo("xxx.xxx.xxxTests")]

However when I call this in my test:

mockApplicationBuilder.Verify(y => y.UseMiddleware<HttpExceptionMiddleware>());

I still get

HttpExceptionMiddleware is inaccessible due to its protection level

Why is my AssemblyInfo attribute not working?

My setup is that I have a solution with 2 folders (src, and tests) and those folders have the src project and the test project in root of the folder respectively. The AssemblyInfo.cs file is in my properties of my src project. Do I need to specify a full path to the test project in my assemblyInfo.cs attribute?

There is an AssemblyInfo.cs in my test project too, although I think that is irrelevant.

like image 433
BeniaminoBaggins Avatar asked Feb 27 '17 18:02

BeniaminoBaggins


1 Answers

Without getting to poke around in your solution I can only offer suggestions

  • Verify your ASP project is referenced in your test project, which is should be or you would have other issues, and most likely as a project reference
  • Verify your test project assembly name and that InternalsVisibleTo contains the fully qualified assembly name
  • Verify that HttpExceptionMiddleware is actually marked as internal
  • Clean and verify the clean, then rebuild your solution.
like image 98
ninja coder Avatar answered Sep 16 '22 16:09

ninja coder