MSDN > 論壇首頁 > Visual Studio Team System - Testing > Are list accessors working in 2008?
發問發問
 

已答覆Are list accessors working in 2008?

  • Tuesday, 2 September, 2008 19:10EricAll 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    Hello-

     

    I just finished running through the TFS intro labs, and decided to try making a new unit test.  The Board class in the TicTacToe example exposes a Cells property that's of type List<Cell>.  Both this property and the Cell type are internal to the app assembly, so I right-clicked in the Board's editor and said "Create Private Accessor" like the demos suggested.

     

    In my unit test, I then added:

    Board_Accessor board = new Board_Accessor();

     

    Cool so far, but the next line will throw a type cast exception:

    object cell = board.Cells[3];

     

    It seems that the generated List<Cell_accessor> and the original List<Cell> types are not compatible.

     

    Is this expected?  Is there a workaround?

    Thanks,

    Eric

解答

  • Wednesday, 3 September, 2008 18:19Joe Allan Muharsky - MSFT 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

     

    Hi Eric,

     

    There are issues with doing deep copies of generic arrays and lists in the publicize functionality, much of it deriving from the .NET behavior you indicated; when A : B, List<A> is not compatible with List<B>.  As you're using internal members, however, the easiest path for you would be to make your assembly's internal visible to the test assembly, and then public accessors will not be neccessary at all for test code making use of internal members.  You can read more about InternalsVisibleTo at http://msdn.microsoft.com/en-us/library/0tke9fxk.aspx.  At a high level, the implementation will look like:

     

    [assembly:InternalsVisibleTo("MyTestAssembly")]

    Hope this helps,

所有回覆

  • Wednesday, 3 September, 2008 18:19Joe Allan Muharsky - MSFT 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

     

    Hi Eric,

     

    There are issues with doing deep copies of generic arrays and lists in the publicize functionality, much of it deriving from the .NET behavior you indicated; when A : B, List<A> is not compatible with List<B>.  As you're using internal members, however, the easiest path for you would be to make your assembly's internal visible to the test assembly, and then public accessors will not be neccessary at all for test code making use of internal members.  You can read more about InternalsVisibleTo at http://msdn.microsoft.com/en-us/library/0tke9fxk.aspx.  At a high level, the implementation will look like:

     

    [assembly:InternalsVisibleTo("MyTestAssembly")]

    Hope this helps,