.Net Core - Testing Internal Methods

.Net Core - Testing Internal Methods

In the .Net Framework, the approach to unit testing internal methods or types was to add an InternalsVisibleTo attribute into the AssemblyInfo.cs file. For example look at the following line of code below:

[assembly: InternalsVisibleTo("Animals.Test")]

This all works fine in the .net Framework. However in .Net Core most of the settings in the AssemblyInfo file have moved to the project file. So to test internal methods, you have 2 options.

Project File Change

Assuming we have a project called Animals and a unit test project called Animals.Tests. The Animals project file which contains the internal method can be modified in visual studio with the following:

  <ItemGroup>

    <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">

      <_Parameter1>$(MsBuildProjectName).Test</_Parameter1>

    </AssemblyAttribute>

  </ItemGroup>

Source File change

The alternative way is to use an attribute in the source file which contains the internal method. For example see the code below:

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Animals.Test")]

namespace Animals

{

    public class Dog

    {

        internal string Chase(string animal)

        {

            return $"Dog chases {animal}";

        }

    }

}

Conclusion

Out of the two methods above, I much prefer the project file approach, since this is the most flexible. I don't like the source file approach, since this would mean sprinkling the attribute over the source code.

To view or add a comment, sign in

More articles by Justin Bannister

  • Why Should I Write Unit Tests?

    In my previous article I explored the reasons why developers don't to write unit tests, when they are developing code…

    2 Comments
  • Why Developers Don't Write Unit Tests

    When I started my career in software development 22 years ago this year, it seemed that unit testing adoption was…

    5 Comments
  • What is Google Glass?

    So back in June 2014, I was invited to try out Google Glass at an event in London. So I made an appointment to go after…

  • Junior Developer Job Hunting Tips

    Last year some junior developers which I have worked with in the past asked me for some job hunting tips. I decided to…

  • Nullable Reference Types

    Previously in C#, references have been allowed to be null and they could be de-referenced without null checks. We would…

    2 Comments
  • Read-only Auto Properties

    In this article I discuss read-only automatic properties. I am going to cover how automatic properties have changed…

  • String Interpolation

    For years we have all used string.Format, it was one of those constructs that I thought "oh wow", when I first started…

  • nameof Operator

    The nameof operator was introduced in C# 6. But before I discuss what the nameof operator is for, I would like to show…

    3 Comments
  • C# Expression Bodied Members

    We first saw expression bodied members introduced in C# 6. This introduced the ability to take a single expression…

    2 Comments

Explore content categories