Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build failure in unit test project with accessors of a project containing covariant types

I added a covariant interface to our project:

interface IView
{
}

interface IPresenter<out TView> where TView : IView
{
    TView View { get; }
}

I created some classes, implementing these interfaces:

class TestView : IView
{
}

class TestPresenter : IPresenter<TestView>
{
  public TestView View
  {
    get { return something; }
  }

  private void DoSomething()
  {
  }
}

And I can use this without problems:

IPresenter<IView> presenter = new TestPresenter();

So everything seems right, so I assume my covariance usage is correct. Unfortunately our unit test projects contain private accessors from some types located in the same project like the covariant interface, which causes a build failure.

Could not load type 'GenericInheritanceTest.IPresenter_Impl`1' from assembly 'GenericInheritanceTest_Accessor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' because it declares a covariant or contravariant type parameter and is not an interface or delegate.

What exactly is the problem here? Is there a failure in my implementation, resp. how to fix this? Can not be, that we have to avoid accessors as soon as we use covariant types??? Is it possible to prevent creating accessors for certain types to solve this problem?

like image 288
Enyra Avatar asked Sep 08 '10 09:09

Enyra


People also ask

Should unit tests be in the same project?

Put Unit tests in the same project as the code to achieve better encapsulation. You can easily test internal methods, which means you wont make methods public that should have been internal.

What is a unit test project?

Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. This testing methodology is done during the development process by the software developers and sometimes QA staff.


1 Answers

This is a bug in Visual Studio 2010. It has been reported to Microsoft Connect but has been closed and will apparently not be fixed.

According to a blog entry by Bruce Taimana development of the private accessor feature has been stopped and may be removed in future versions of Visual Studio. Possible alternatives listed are:

  1. Use the Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject class to assist in accessing internal and private APIs in your code. This is found in the Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll assembly.

  2. Create a reflection framework that would be able to reflect off your code to access internal or private APIs.

  3. If the code you are trying to access is internal, you may be able to access your APIs using the InternalsVisibleToAttribute so your test code can have access to the internal APIs.

like image 114
Martin Liversage Avatar answered Oct 11 '22 07:10

Martin Liversage