2010-02-05 66 views
0

我正在寫一個應用程序,使用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得到由框架調用時分派的事件管理。

在其他容器/框架環境中,我傾向於不編寫運行框架責任的測試,因爲(恕我直言)這會導致脆弱的測試。但是,由於缺乏編譯器檢查,在這種情況下它可能看起來是有爭議的。

想法?

回答

0

如果你仔細想一想,你並沒有真正地測試框架的責任,就像測試編程人員可以輸入的效果一樣。但是,如果編寫該事件的相同編碼人員也編寫了測試,並且如果事件名稱是不會經常更新的東西,那麼您可能會跳過它,因爲任何錯別字很可能會在測試正在編寫中。

0

你想測試的聲音就像集成測試一樣。如果你想要一個單元測試,你必須定義你的單元是什麼。

如果你想測試你的事件,模擬事件接收器,並找出是否之後調用模擬。

public class MockLoginCommand : ICommandReceiver { 

    public bool BeenCalled { get; set; } 

    [MessageHandler] 
    public function execute(attemptLoginEvent:AttemptLoginEvent):void { 
     BeenCalled=true; 
    } 
}