我正在寫一個應用程序,使用MVC框架來照顧我們系統的很多樣板佈線。具體來說 - 應用程序使用Parsley MVC框架以Flex編寫。但是,這個問題不是特定於語言的。務實單元測試
在我的演示模型/代碼隱藏/視圖 - 控制器(隨便你怎麼稱呼它),我可能有這樣的事情:
[Event(name="attemptLogin",type="com.foo.AttemptLoginEvent")]
[ManagedEvents["attemptLogin"]
public class LoginViewPM {
public function attemptLogin(username:String,password:String):void
{
dispatchEvent(new AttemptLoginEvent(username,password));
}
}
然後,其他地方在我的系統,代碼響應這看起來像這樣
public class LoginCommand {
[MessageHandler]
public function execute(attemptLoginEvent:AttemptLoginEvent):void {
// Do login related stuff
}
}
重要的是要注意,在Flex/Actionscript中,編譯器不會檢查Metatags。例如:
[Event(name="attemptLogin",type="com.foo.AttemptLoginEvent")]
[ManagedEvent["attemptLogin"] // Spelling mistake - metatag is ManagedEvents
public class LoginViewPM {
和
[Event(name="attemptLogin",type="com.foo.AttemptLoginEvent")]
[ManagedEvent["attemtLogin"] // Spelling mistake - event name is wrong
public class LoginViewPM {
在上述兩個例子中,框架將失敗。在第一個例子中,它默默地失敗了(因爲元標記是不正確的 - 因此框架從不參與)。在第二個例子中,我們得到了一些運行時記錄,部分警告我們事情是錯誤的。
鑑於此,關於MVC框架的職責,PM的attemptLogin()方法的單元測試的實用級別是什麼?即:
我應該:
- 測試該AttemptLoginEvent由MVC框架
- 測試的LoginCommand得到由框架調用時分派的事件管理。
在其他容器/框架環境中,我傾向於不編寫運行框架責任的測試,因爲(恕我直言)這會導致脆弱的測試。但是,由於缺乏編譯器檢查,在這種情況下它可能看起來是有爭議的。
想法?