2015-10-03 87 views
1

我在C#中使用了一些COM庫,它綁定到特定的硬件,沒有它就無法工作。在開發/測試計算機上,我沒有那個硬件。這是使用庫的方法是這樣的:我可以模擬不可能實例化的對象嗎?

using HWSysManagerLib; 
bool ProcessBias(HWSysManager systemManager, string hwPath) 
{ 
    int handle = systemManager.OpenConfiguration(hwPath); 
    ... 
    // some magic goes here 
    // return result 
} 

的問題是,我可以爲測試方法和如何嘲笑HWSysManager?在HWSysManager中只有很少的方法,並且模擬它們的測試功能並不是問題。如果可能的話,一個很小的例子就是如何嘲笑它。

回答

5

你可以在這裏使用適配器模式。

創建一個名爲接口IHWSysManager

public interface IHWSysManager 
{ 
    int OpenConfiguration(string hwPath); 
} 

真正的實現類僅委派工作圖書館:

public class HWSysManagerImpl : IHWSysManager 
{ 
    private HWSysManager _hwSysManager; //Initialize from constructor 

    public int OpenConfiguration(string hwPath) 
    { 
     return _hwSysManager.openConfiguration(hwPath); 
    } 
} 

使用的界面在你的代碼是這樣的:

bool ProcessBias(IHWSysManager systemManager, string hwPath) 
{ 
    int handle = systemManager.OpenConfiguration(hwPath); 
    ... 
    // some magic goes here 
    // return result 
} 

現在你可以模擬你的IHWSysManager接口與莫ck框架,或者你可以自己創建一個存根類。

+0

所以改變我的應用程序是不可避免的。即使如此,我如何從我的應用程序中調用「ProcessBias」?現在我稱之爲:'HWSysManager sm = new HWSysManager(); ProcessBias(sm,「」);' – Pablo

+0

基本上我必須實現所有方法委託,我使用該庫。 – Pablo

+0

您不應該在調用方法中創建實例。改爲從構造函數中獲取IHWSysManager。在你的開發機器中給出存根類作爲參數,並在實際環境中給出真正的實現類。這種技術被稱爲依賴注入,你可以閱讀關於它[這裏](http://stackoverflow.com/questions/130794/what-is-dependency-injection)。 –

-1

我想你應該爲你的模擬案例創建HWSysManager(或其他名稱)類,添加一些想要的方法,然後嘲笑它們。例如:

class HWSysManager 
    { 
     public virtual int ExampleReturnIntMethod(int a) 
     { 
      var someInt = 0; 
      return someInt; 
     } 

,然後設置:

public void TestMethod() 
    { 
     Mock<HWSysManager> hwSysManager = new Mock<HWSysManager>(); 
     hwSysManager.Setup(x => x.ExampleReturnInMethod(It.IsAny<int>())).Returns(10); //if parameter is any of int, return 10 in this case 
    } 

然後使用您的嘲笑對象只使用 '對象' 屬性:

var hwSysInstance = hwSysManager.Object; 
var result = hwSysInstance.ExampleReturnInMethod(2); //result will be 10 in this case - as we have mocked 

在殼體上方的方法/屬性有是虛擬的。

可以使用接口也,你的情況:

public interface HwsysManager 
    { 
     int OpenConfiguration(string hwPath); 
    } 

    public void TestMethod() 
    { 
     Mock<HwsysManager> hwsysManager = new Mock<HwsysManager>(); 

     hwsysManager.Setup(x => x.OpenConfiguration(It.IsAny<string>())).Returns(10); 
    } 

這個模擬庫的所有功能描述如下: https://github.com/Moq/moq4/wiki/Quickstart

+0

其實我需要測試使用'HWSysManager'的方法。所以如果我按照你的建議,我得到一個類型錯誤:無法從'Moq.Mock '轉換爲'HWSysManagerLib.HWSysManager'。在TestMethod中,我有'bool b = ClientProgram.ProcessBias(tcsm)'。 'tcsm是模擬對象。所以我懷疑在這裏作弊是不可能的? – Pablo

+0

所以tcsm實際上是tcsm.Object,但仍然是相同的錯誤。 – Pablo

+0

此外,我在TestClass中有一個錯誤,那就是沒有引用定義'HWSysManagerLib.HWSysManager'的程序集。 – Pablo

1

您可以使用Typemock Isolator來僞造HWSysManager。

對於你的榜樣,你可以做到以下幾點:

var fakeManager = Isolate.Fake.Instance<HWSysManager>(); 

Isolate.WhenCalled(() => fakeManager.OpenConfiguration("")).WillReturn(0); 

然後,你可以通過這個僞造經理ProcessBias(IHWSysManager systemManager, string hwPath)作爲參數。

如上所述,您可以模擬IHWSysManager中的幾種方法。所以,我的建議是成立的這個經理人的方法使用DoInstead()行爲:

Isolate.WhenCalled(() => fakeManager.OpenConfiguration("")).DoInstead(
    context => 
    { 
     //Your simulation 
    }); 

你可以看看here以獲取更多信息。我想,它對你來說可能非常有用。

相關問題