Mocking internal types

Depending on your project requirements, you might need to create mock objects for internal types. Internal types or members in C# are accessible only within files in the same assembly. Mocking internal types can be done by adding custom attributes to the AssemblyInfo.cs file of the concerned projects.

If the assembly containing the internal types does not already have the AssemblyInfo.cs file, you can add it. Also, when the assembly is not strongly-named, you can add the InternalsVisibleTo attribute, which has the public key excluded. You have to specify the name of the project to share visibility with, which in this case should be the test project.

If you change the LoanService access modifier to internal, you will ...

Get C# and .NET Core Test Driven Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.