2010-02-05 40 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; 
    } 
}