3

I have a unit test class like the following:

[TestClass]
internal class AttractionRepositoryUnitTest : 
             RepositoryBaseTest<AttractionRepository, Attraction, AttractionFakeRepository>
{
    //Code here
}

The tests inside those class are never found due to the internal modifier. To make it visible, one of the classes inside the RepositoryBaseTest has to become public, which is undesirable and to avoid that, I would need a refactorization that would take some time. Is it possible to make this test class visible to the unit tests without modifying it's visibility?

The Test class is the one that it is internal.

9
  • I don't think it's a duplicate, @ErikE - from the source code sample you can see that the test class is the one that is internal Commented Jul 16, 2018 at 17:09
  • 2
    Solution A: change the unit test class from internal to public. Honestly, why is it undesirable? Solution B: use a unit test framework that do support internal classes, such as NUnit Commented Jul 16, 2018 at 17:14
  • @ricardo-alves you might need to change the title of the question, it's a bit misleading.... :-) Commented Jul 16, 2018 at 17:16
  • @RuiJarimba Just did it :P Commented Jul 16, 2018 at 17:18
  • 1
    @RicardoAlves it would be really interesting to learn what T is and why it has to be internal. What possible unit test concern has to be internal to a unit test project? Your tests are in a different project from the code they’re testing, right? Commented Jul 16, 2018 at 18:21

2 Answers 2

3

I believe what your looking for is https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute(v=vs.110).aspx

InternalsVisibleToAttribite

Add this attribute to where you would like the internals to be visible to. Use multiple attributes where necessary for multiple assemblies.

If you can't add that attribute you will need to employ reflection to access that which is in question.

Sign up to request clarification or add additional context in comments.

5 Comments

And to what Assembly should I make it visible to? I think I already tried every Microsoft Assembly.
@Jay from the source code sample you can see that the test class is the one that is internal
Updated answer, if you can't add the attribute to the source you'll have to use reflection
@Jay are you sure that will help?? His problem is that the unit test framework cannot find the tests because the test class is marked as internal. Please note that he is NOT trying to unit test an internal class in a different assembly
Yes, you would make the internals visible to the unit test assembly... why do you think that matters I don't know but this is the answer. You may need a 2nd attribute for the main entry point assembly as well so add 2 internals visible yo attributes.
1

Assuming that all type parameters of RepositoryBaseTest implement some interface you can just provide public implementations of these interfaces that wrap the internal implementations:

public AttractionRepositoryTestWrapper : IAttractionRepository
{
    private AttractionRepository _attractionRepositoryImpl;

    // forward all calls to _attractionRepositoryImpl
}

The same for other internals if needed.

Then RepositoryBaseTest can be made public, and thus AttractionRepositoryUnitTest as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.