2012-09-09 57 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