Search code examples
c#compiler-errorsnunitmstest

Inconsistent accessibility error using NUnit but compiles fine using only MSTest?


Note: I checked the first two pages of search results that the Ask Question page brought up, but none address the issue/question I have.


Update: Having restarted VS after a good night's rest, the inconsistency is gone...

Going back to the commit with the code that did not produce the error before, and even going as far as deleting the bin and obj folders, now of course dutifully produces the error consistently and I can only make it go away by changing the code - as it should be.

Which makes me both :D and :(

I do dislike this kind of inconsistent behavior!


Why would code that compiles fine using MSTest, suddenly throw

Error 1 Inconsistent accessibility: return type 
  'ActualProject.ClassHierarchy.BaseJointer' is less accessible than method
  'TestProject.ClassHierarchy.BaseJointer_Tests.MakeJointer()'

when adding NUnit into the mix?

The class that is "less accessible":

class BaseJointer
{
    public DoveTailJoint MakeDoveTailJoint(
        PieceOfWood woodTails, PieceOfWood woodPins) 
    { return null; }
}

than the method:

[TestClass]
public class BaseJointer_Tests
{
    protected virtual BaseJointer MakeJointer()
    {
        return new BaseJointer();
    }
}

The error does not occur when none of the projects references NUnit.

The error does occur in two scenarios:

  1. When there are two projects in the solution: ActualProject and TestProject. TestProject already referenced MSTest. The error occurred when adding a reference to NUnit to the TestProject.
  2. When there are three projects in the solution: ActualProject, TestProject only referencing MSTest and NUnitTestProject only referencing NUnit.

The second one has me especially baffled as it produces an error on (the code of) the TestProject which doesn't reference NUnit at all.

FYI: ActualProject has InternalsVisibleTo set to both TestProject and NUnitTestProject

Why would internal become less visible than protected by sole virtue of adding a reference to the NUnit test framework?


Solution

  • The error does not occur when none of the projects references NUnit.

    I think that is incorrect.

    This error should occur always. The (return) type should be at least as accessible as the method (member).

    In the partial ordering of accessibility levels, the two levels internal and protected are not mutually comparable. An internal member can be seen by someone (non-deriving type in the same project) who cannot see the protected member. And the protected member can be seen by someone (deriving type in a distinct project) who cannot see the internal one.

    So internal is not "as least as accessible as" protected. Therefore the compile-time error is correct and required.

    Why would internal become less visible than protected by sole virtue of adding a reference to the NUnit test framework?

    It should not. Are you sure you forced a compilation of all projects before you added the project reference? Maybe the project was not recompiled until you did something (unrelated) to it?