在C#中測試私有方法的單元Visual Studio允許通過自動生成的訪問器類對私有方法進行單元測試。我編寫了一個成功編譯的私有方法的測試,但它在運行時失敗了。一個相當小的代碼和測試版本是://in project MyProjclass TypeA{
private List<TypeB> myList = new List<TypeB>();
private class TypeB
{
public TypeB()
{
}
}
public TypeA()
{
}
private void MyFunc()
{
//processing of myList that changes state of instance
}} //in project TestMyProj public void MyFuncTest(){
TypeA_Accessor target = new TypeA_Accessor();
//following line is the one that throws exception
target.myList.Add(new TypeA_Accessor.TypeB());
target.MyFunc();
//check changed state of target}運行時錯誤是:Object of type System.Collections.Generic.List`1[MyProj.TypeA.TypeA_Accessor+TypeB]' cannot be converted to type 'System.Collections.Generic.List`1[MyProj.TypeA.TypeA+TypeB]'.根據intellisense - 因此我猜編譯器 - 目標是TypeA_Accessor類型。但在運行時它的類型為TypeA,因此列表添加失敗。有什么辦法可以阻止這個錯誤嗎?或者,或許更有可能的是,其他人有什么其他建議(我預測可能“不測試私有方法”和“沒有單元測試操縱對象的狀態”)。
- 3 回答
- 0 關注
- 873 瀏覽
添加回答
舉報
0/150
提交
取消