我正在写一个应用程序,使用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得到由框架调用时分派的事件管理。
在其他容器/框架环境中,我倾向于不编写运行框架责任的测试,因为(恕我直言)这会导致脆弱的测试。但是,由于缺乏编译器检查,在这种情况下它可能看起来是有争议的。
想法?