2
我正在尋找一個框架或內置的方式來編寫優雅的代碼,很容易維護,執行以下操作。如何使用框架或內置功能執行代碼作爲步驟?
這是例子:
在我的主要方法,我想要做的三件事情:
- 購買蛋糕
- 蛋糕上的
- 即成蛋糕
只有一次所有3個動作完成後,我是否認爲該操作是成功的。 如果任何一個特定操作失敗,我想知道失敗的原因。
爲了解決這個問題,我的主要方法可以最終看起來是這樣的:
public void IsBirthdayAsuccess()
{
var birthdayManager = new birthdayManager();
try
{
var cake = birthdayManager.BuyCake(price.cheap, quality.good, delivery.fast);
Assert.isTrue(cake.ArrivedQuick && cake.IsGood && cake.IsQuick, "Supplier didn't get the cake right");
}
catch
{
Assert.Fail("The birthday operation has failed on step 1 : Buying the cake failed");
}
try
{
birthdayManager.Cakes[0].PutIcingOn("Vanilla");
Assert.IsTrue(birthdayManager.Cakes[0].IsIced(), "Icing the cake didn't work");
}
catch
{
Assert.Fail("The birthday operation has failed on step 2 : Putting icing on the cake");
}
try
{
Party.ServeCake(birthdayManager.Cakes[0]);
Assert.IsTrue(birthdayManager.Cakes[0].IsServed(), "Cake couldn't be served");
}
catch
{
Assert.Fail("The birthday operation has failed on step 3 : Serving the cake");
}
}
現在,在現實中,「步驟」在這裏是通過一系列的代碼try/catch塊的表示,但實際上在企業系統的其他地方。這就是爲什麼我正在尋找一個框架,可以在函數內提供更一致的「步驟」框架實現。
我正在考慮滾動自己,但也許還有其他的東西存在。
如果我滾我自己也將結束是這樣的:
public void TestMethod(int id)
{
var test = new test(id);
TestSteps
{
["Login to application"]
TestStep
{
test.LoginToApplication();
}
Assert, "You are not logged in"
{
test.IsLoggedIn();
}
Failure
{
Assert.Fail("Test failed on step " + testContext.TestStep.Index);
}
}
難道這樣的一些框架存在嗎?
編輯:也可以重複使用,我將能夠遍歷「步驟」集合和「步驟」項目,這意味着我可以使用它在Excel或其他地方進行報告。編輯2:「滾動你自己」的例子旨在取代try/catch和流控制的語義,是C#這可擴展嗎?
[責任鏈](http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern)是否適用於您的案件? [.net的鏈接](http://www.dofactory.com/net/chain-of-responsibility-design-pattern) – MVCDS 2014-09-10 14:18:47