MSDN > Home page del forum > Visual Studio Team System - Testing > Are list accessors working in 2008?
Formula una domandaFormula una domanda
 

Con rispostaAre list accessors working in 2008?

  • martedì 2 settembre 2008 19.10EricAll Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     

    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

Risposte

  • mercoledì 3 settembre 2008 18.19Joe Allan Muharsky - MSFT Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta

     

    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,

Tutte le risposte

  • mercoledì 3 settembre 2008 18.19Joe Allan Muharsky - MSFT Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta

     

    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,