對不起,但我從來沒有用過NUnit.Mocks - 但我確實有一些NMock和Moq的經驗[順便說一下,我強烈推薦]。通常,您使用模擬庫來生成接口定義的代理,並且我假設NUnit.Mocks以相同的方式運行。
因此,如果您想嘲笑你單身,你將有可能做到以下幾點,
一個。創建一個接口,說
// All methods you would like to mock from this class, should
// be members of this interface
public interface IWebSiteConfiguration
{
// Should match signature of method you are mocking
CodeType getCodeByCodeNameAndType (
string codeString,
CatalogType catalogType);
}
b。 「實施」界面
// You've already written the method, interface matches signature,
// should be as easy as slapping interface on class declaration
public class WebSiteConfiguration : IWebSiteConfiguration { }
c。消費界面
好吧,所以步驟c。是你大部分工作的地方。從邏輯上講,如果你嘲笑你的單身人士,你實際上是對消費者進行單元測試[你從你的樣本中遺漏了的東西]。對於c。只需向消費者的ctor添加一個參數,或添加Type'IWebSiteConfiguration'的可公開訪問的屬性,然後在內部引用實例成員並針對此新接口調用您的方法。考慮這一點,
是
public class MyClass
{
public MyClass() { }
public void DoSomething()
{
// bad singleton! bad boy! static references are bad! you
// can't change them! convenient but bad!
code = WebSiteConfiguration.Instance.getCodeByCodeNameAndType (
"some.string",
someCatalog)
}
}
成爲
public class MyClass
{
private readonly IWebSiteConfiguration _config = null;
// just so you don't break any other code, you can default
// to your static singleton on a default ctor
public MyClass() : this (WebSiteConfiguration.Instance) { }
// new constructor permits you to swap in any implementation
// including your mock!
public MyClass (IWebSiteConfiguration config)
{
_config = config;
}
public void DoSomething()
{
// huzzah!
code = _config.getCodeByCodeNameAndType ("some.string", someCatalog)
}
}
在單元測試中,創造了模擬,通過模擬消費者的參考,並測試消費者。
[Test]
public void Test()
{
IWebSiteConfiguration mockConfig = null;
// setup mock instance and expectation via
// NUnit.Mocks, NMock, or Moq
MyClass myClass = new MyClass (mockConfig);
myClass.DoSomething();
// verify results
}
這還用作實用介紹依賴注入[DI]。這只是將服務的引用(例如,您的網站配置類)傳遞或「注入」到消費者的做法,而不是讓消費者直接調用服務[例如通過靜態單例類]。
希望這有助於:)
如果這可以刺激你的胃口,一些快速參考進一步閱讀。 Martin Fowler依賴注入[DI],控制反轉[IoC]和容器[http://martinfowler.com/articles/injection.html] Castle Windsor容器快速教程[http:// dotnetslackers。 com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart1.aspx] – 2009-10-21 17:59:13