0

I have a solution which contains two assemblies, both in VB.Net. One assembly contains my code, which mainly includes Friend Classes; and the other assembly contains code dedicated toe unit testing the first assembly. I rely on MSTest.

How can I Unit Test my Friend Classes?

I have seen across several tutorials and documentation that C# has the <Assembly(): InternalsVisibleTo("AssemblyName")>, but I understand this is not available for VB.

Whilst searching on SO, I only found one match, which suggests an interesting workaround. I am willing to follow this alternative if there is no better option. I am however very surprised there is no clean way to achieve something I though was quite elementary.

Ama
  • 1,373
  • 10
  • 24
  • I have never used it, but as far as I know that attribute works in VB. See [this answer](https://stackoverflow.com/a/2791331/1359668) for more info. – Steven Doggart May 03 '19 at 19:10
  • Interesting.. My target framework is set to 4.0 so it should work. However, the VS compiler does not recognize the attribute. Thanks for the hint though, now I have a new thread to investigate! – Ama May 03 '19 at 19:27
  • If you are targeting the .NET Framework, you'll need to add a reference to `System.Runtime.dll`. Regardless of your framework, you'll either need to fully qualify it or import the `System.Runtime.CompilerServices` namespace. – Steven Doggart May 03 '19 at 19:34
  • As per the MS documentation, indeed. Still a bit confused with Attribute Classes, did not know they worked like regular classes in terms of namespaces. Thanks, you nailed it! – Ama May 03 '19 at 19:43
  • @Ama - attributes are regular classes, you can create your own. .NET Framework 4.0? - this is obsolete framework, not supported by Microsoft anymore. – Fabio May 04 '19 at 08:21

1 Answers1

1

You are correct that, at one point in time, the InternalsVisibleToAttribute was only available in C# and did not work in VB. However, you are incorrect in assuming that it still doesn't work in VB today. The time when that attribute didn't work in VB has long since past. Unless you are using excessively old tools, that attribute will work in VB, just as it does in C#.

If it's not working for you, it's almost certainly for a different reason. If it fails to compile, saying that the type doesn't exist, that's either because you aren't referencing the necessary assembly or you aren't importing its namespace. If your project is targeting .NET Core or .NET Standard, you shouldn't need to reference any additional assemblies, but if you are targeting the .NET Framework, you'll need to add a reference to the System.Runtime.dll (if it isn't already referenced). And then, regardless of which framework you are targeting, you either need to fully qualify the class name or you need to import it's namespace, which is System.Runtime.CompilerServices.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105