- Home /
InternalsVisibleTo in Unity
Hi Unity members,
For unit testing I am trying to use InternalsVisibleTo("assemblyname") in Unity 3d without any success.
Has anyone ever used it in Unity ?
Here is the problem.
[assembly: InternalsVisibleTo("Test")]
namespace Dev
{
public class Example
{
internal bool Function(){ //do stuff }
}
}
namespace Test
{
public class TestExample
{
Example example = new Example();
bool value = example.Function();//Error Cannot access "Function" due to its
//protection level
}
}
When I open this project in .Net 2008, .Net compiler does not give ANY error.
Note : In the documentation for InternalVisibleTo, its written that this is a new class for .Net 2.
Thanks for reading !!
I'm experiencing a similar issue in my code. I'd like to not have to expose a whole bunch of internal object state as public just for the purposes of testing, while at the same time be able to use it to verify correct internal functioning of the class via automated tests.
I have tried "using System.Runtime.CompilerServices" per the $$anonymous$$SDN documentation, but still get the "type or namespace name 'InternalsVisibleTo' could not be found.' error message. So it would seem that either this is not implemented in Unity's version of the .NET runtime or it is misplaced.
Can anyone shed some further insight into this matter?
Thanks
Hi $$anonymous$$,
Well this is not an answer but I was not able to solve this issue. I simple changed internal to public.
Thanks
Answer by hejaaa · Jun 26, 2020 at 02:32 PM
It is old question, but for answer, I suppose you need use correct assembly name. But for better order assemblies you need create AssemblyInfo.cs
file and there write something like this:
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("UCM_uTests")]
This is necessarily use for Testing. Use just public accessors with various assemblies is wrong because other programmer can implement this code wrong.
Answer by peterpan2022 · Jul 25, 2021 at 07:12 PM
My problem, and I think OPs problem, is the difference between an assembly name and a namespace. We feed a namespace to InternalsVisibleTo. But it is expecting the name of an assembly.
Your answer
Follow this Question
Related Questions
How to separate unit tests into another C# project? 1 Answer
Tests cannot find classes 0 Answers
Can we create playmode tests outside of the main build assembly? 0 Answers
Adding Game Assembly causes type or namespace errors 0 Answers
Test-Runner and Assembly Definitions conflict with Third-Party assets 1 Answer