2012-09-09 52 views
0

嘗試單元測試某個類項目的一些簡單代碼,但是在我的測試代碼中 - 它一直告訴我我的InventorySelect找不到。它問我是否缺少使用聲明等,但就我所見,這一切都是正確的。c#,找不到類型?

我的代碼

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Home 
{ 
    class InventoryType 
    { 

     /// <summary> 
     /// Selects the inventory type and returns the selected value 
     /// </summary> 
     public class InventorySelect 
     { 
      private string inventoryTypes; 
      public String InventoryTypes 
      { 
       set 
       { 
        inventoryTypes = value; 
       } 

       get 
       { 
        return inventoryTypes; 
       } 
      } 


      /// <summary> 
      /// Validate that the inventory is returning some sort of value 
      /// </summary> 
      /// <returns></returns> 
      public bool Validate() 
      { 
       if (InventoryTypes == null) return false; 
       return true; 
      } 
     } 
    } 
} 

我的測試代碼

using System; 
using System.Text; 
using System.Collections.Generic; 
using System.Linq; 
using Microsoft.VisualStudio.TestTools.UnitTesting; 
using Home.InventoryType.InventorySelect; 

namespace HomeTest 
{ 
    [TestClass] 
    public class TestInventoryTypeCase 
    { 
     [TestMethod] 
     public void TestInventoryTypeClass() 
     { 
      InventorySelect select = new InventorySelect(); 
      select.inventoryTypes = "Collection"; 

      if (Validate() = true) 
       Console.WriteLine("Test Passed"); 
      else 
       if (Validate() = false) 
        Console.WriteLine("Test Returned False"); 
       else 
        Console.WriteLine("Test Failed To Run"); 

      Console.ReadLine(); 

     } 
    } 
} 
+3

你意外地嵌套你的類。內部類稱爲'InventoryType.InventorySelect'。 – CodesInChaos

+0

CodesInChaos說,你嵌套你的類。此外,任何C#類的默認可見性都是內部的,因此InventoryType對測試項目不可見。 –

+0

@CodesInChaos發表您的評論作爲回答 –

回答

3

你的類被嵌套創建一個實例,你的外部類的內部(未聲明public)。將內部課程從外部課程中移出,或者1)公開外部課程,2)使用外部課程名稱InventoryType.InventorySelect來限定您對InventorySelect的引用。

+0

外部類是內部不是私人的。 –

+0

Touche。我會糾正的。 –

3

在您的測試項目時,必須參考項目(的裝配)添加到測試。


編輯:您的嵌套類InventorySelect是不聲明爲public類裏面。聲明類InventoryType爲公共。你將不得不與

var select = new InventoryType.InventorySelect(); 
+0

有一個參考家庭項目 – Expecto

2

您的InventoryType班不是public!將其標記爲public並重新編譯它。 C#類默認爲internal