InternalsVisibleTo does not work

2020-02-20 05:43发布

I insert the line:

[assembly: InternalsVisibleTo("MyTests")]

inside my project under test( Properties/AssemblyInfo.cs) where MyTests is the name of the Unit Test project. But for some reason I still cannot access the internal methods from the unit test project.

Any ideas about what I am doing wrong ?

5条回答
何必那么认真
2楼-- · 2020-02-20 06:08

In my case I was coding to an Interface. As interfaces can only specify public properties the internal properties were not present on the interface.

Make sure you're not doing the same thing as I was!

I changed:

Assert.IsNotNull((exportFileManager)?.ThumbnailFileExporter);

To:

Assert.IsNotNull((exportFileManager as ExportFileManager)?.ThumbnailFileExporter);
查看更多
萌系小妹纸
3楼-- · 2020-02-20 06:11

You can expose internals from strong named assembly to another strong named friend assembly only. But non-strong named assembly can expose internals to strong named friend assembly.

查看更多
我只想做你的唯一
4楼-- · 2020-02-20 06:20

If your assembly is signed with a strong name look at this answer.

Otherwise check that the name of your test assembly really is "MyTests.dll" (it doesn't have to match the project name, though it will by default).

查看更多
Evening l夕情丶
5楼-- · 2020-02-20 06:20

You still need your test project to reference your main project.

This can be easy to overlook and if you have no existing test code this may appear like the InternalsVisibleTo is not functioning.

查看更多
Rolldiameter
6楼-- · 2020-02-20 06:29

Lets break it down a bit as many of us has experienced this slight mix-up in the past...

Assembly A has your internal class. Assembly B has your unit tests.

You wish to grant the internals of assembly A visibility in assembly B.

You need to put the InternalsVisibleTo assembly attribute inside assembly A and grant access to assembly B.

查看更多
登录 后发表回答