2014-10-29 45 views
1
public class ConfigSection : ConfigurationSection 
{   
    public static ConfigSection GetConfigSection() 
    { 
     return (ConfigSection)System.Configuration.ConfigurationManager. 
      GetSection("ConfigSections"); 
    } 

    [System.Configuration.ConfigurationProperty("ConstantsSettings")] 
    public ConstantSettingCollection ConstantsSettings 
    { 
     get 
     { 
      return (ConstantSettingCollection)this["ConstantsSettings"] ?? 
       new ConstantSettingCollection(); 
     } 
    } 


    public class ConstantSettingCollection : ConfigurationElementCollection 
    { 

     public ConstantElements this[object key] 
     { 
      get 
      { 
       return base.BaseGet(key) as ConstantElements; 
      } 
      set 
      { 
       if (base.BaseGet(key) != null) 
       { 
        base.BaseRemove(key); 
       } 
       this.BaseAdd(this); 
      } 
     } 

     protected override ConfigurationElement CreateNewElement() 
     { 
      return new ConstantElements(); 
     } 

     protected override object GetElementKey(ConfigurationElement element) 
     { 
      return ((ConstantElements)element).Key; 
     } 
    } 

    public class ConstantElements : ConfigurationElement 
    { 
     [ConfigurationProperty("key", IsRequired = true)] 
     public string Key 
     { 
      get 
      { 
       return this["key"] as string; 
      } 
     } 

     [ConfigurationProperty("val", IsRequired = true)] 
     public string Constants 
     { 
      get { return this["value"] as string; } 
     }   
    }   
} 

public class ConstantHelper 
{ 
    public static string ConstantForLog 
    { 
     get 
     { 
      return ConfigSection.GetConfigSection().ConstantsSettings["ConstantForLog"].Constants; 
     } 
    } 
} 

以上全新的單元測試是從應用配置中讀取一些常量值的代碼 這裏是我的代碼在構造函數中賦值的值。如何對使用靜態類,然後使用ConfigurationElementCollection的方法進行單元測試?

public class HomeController 
{ 
    protected string constants; 
    public HomeController() 
    { 
     constants = ConstantHelper.ConstantForLog; 
    } 
} 

測試代碼

[TestClass] 
public class HomeControllerTester 
{ 
[TestMethod] 
public void Initialize_Tester() 
    { 
    //Creating Instance for the HomeController 
    HomeController controller = new HomeController(); 
    } 
} 
在調試

發現的AppSettings不被ConstantHelper類閱讀

解決方案

找到解決方案實際上它工作正常的錯誤是在應用程序完成.config

此外,我所面臨的另一個問題是在ConfigSection 對於MVC應用程序的web.config沒有必要爲名稱空間類型=」 其中作爲用於單元測試的app.config有必要命名空間類型=’類型,_namespace」

+0

也添加了app.config文件到測試項目 – Krishjs 2014-10-29 07:05:24

回答

4

你將不得不注入ConstantHelperHomeController。你可以將它連接起來然後注入它。從單元測試通過IConstantHelper的模擬對象。

UPDATE

我已經定義了ConstantHelper類的接口給我一個機會注入和嘲笑的依賴。

ConstantHelper.cs

public class ConstantHelper : IConstantHelper 
{ 
    public string ConstantForLog 
    { 
     get 
     { 
      return ConfigSection.GetConfigSection().ConstantsSettings["ConstantForLog"].Constants; 
     } 
    } 
} 

public interface IConstantHelper 
{ 
    string ConstantForLog { get; } 
} 

HomeController.cs

注意我現在從外面所以單元測試可以模擬其注入恆幫手。

public class HomeController : Controller 
{ 
    private readonly IConstantHelper _constantHelper; 

    public HomeController(IConstantHelper constantHelper) 
    { 
     _constantHelper = constantHelper; 
    } 

    public ActionResult Index() 
    { 
     return View(_constantHelper.ConstantForLog); 
    } 
} 

HomeControllerTest.cs

[TestClass] 
public class HomeControllerTest 
{ 
    [TestMethod] 
    public void Index_WithDependecySetupCorrectly_ReturnTestString() 
    { 
     var mockHelper = new Mock<IConstantHelper>(); 
     const string testDataString = "TestString"; 
     mockHelper.Setup(z => z.ConstantForLog).Returns(testDataString); 

     //Creating Instance for the HomeController 
     var controller = new HomeController(mockHelper.Object); 

     var result = controller.Index() as ViewResult; 

     Assert.IsNotNull(result); 
     Assert.AreEqual(testDataString, result.ViewName); 
    } 
} 

我用的起訂量嘲笑框架。只需在您的測試項目在包管理器控制檯中使用以下命令來安裝它:

安裝,包裝起訂量

希望這有助於。

+0

可以請你解釋一下示例 – Krishjs 2014-10-29 09:21:59

+0

@Krishjs - 查看我的更新。 – SBirthare 2014-10-29 10:13:01

+0

感謝您的解決方案,這也幫助了我,花了兩天的時間獲得基本的權利,它解決了我的問題。上面有一些虛擬成員的問題 – Krishjs 2014-10-31 13:10:21

相關問題