2014-03-25 325 views
4

我正在使用Visual Studio 2012,並且出現以下錯誤:「錯誤'hw02.World'不包含帶有2個參數的構造函數我嘗試創建單元測試我的課:Visual Studio 2012 - 單元測試錯誤

class World : IWorld 
{ 

    public World(int width, int height) 
    { 
     Width = width; 
     Height = height; 
     world = new IBuilding[width, height]; 
    } 
} 

其產生錯誤的測試:

[TestClass] 
public class UnitTest1 
{ 
    [TestMethod] 
    public void TestMethod1() 
    { 
     var world = new World(5, 5); // this line creates the error: 'hw02.World' does not contain a constructor that takes 2 arguments 
    } 
} 

感謝所有幫助

+3

你是否確定*它正在看同一個'World'類?你的第一堂課肯定是'hw02.World'? –

+0

世界不公開嗎? – jvilalta

回答

4

世界是一個內部類(沒有在類一級公共關鍵字) , 如果你的單元測試不在同一個程序集中,並且如果這些程序集之間沒有關係,您的單元測試將看不到任何公共構造函數,因此抱怨沒有構造函數帶有兩個參數(從測試程序集的角度來看,這是真正)。

請參閱關於Default Visibility的文檔。

要麼使class World a public class World或將InternalsVisibleTo屬性添加到包含World類的程序集。

相關問題