8

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.

BeniaminoBaggins
  • 11,202
  • 41
  • 152
  • 287
  • 2
    Seems correct. It may be something as obvious as a mismatch between "xxx.xxx.xxxTests" and the actual test project's full name. Or maybe you need to rebuild the project which you're testing. As far as I know, you don't need to specify a full path to your project. – evilkos Feb 27 '17 at 18:46
  • Are you using Moq or some other NuGet package for doing your `mockApplicationBuilder`? If so, you need to expose internals to that assembly as well. – Micteu Sep 14 '17 at 17:12

2 Answers2

2

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.
ninja coder
  • 1,067
  • 9
  • 22
0

A possible answer is that the AssemblyInfo.cs file is being auto-generated (so overwriting or ignoring yours). Very much a guess. See also:

Equivalent to AssemblyInfo in dotnet core/csproj

debater
  • 466
  • 6
  • 11