务实的单元测试

发布于 2024-08-20 08:26:36 字数 1474 浏览 4 评论 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 中,编译器不会检查元标记。例如:

[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 上的 attemptsLogin() 方法的单元测试的实用级别是什么?即:

我应该:

  • 测试 AttemptLoginEvent 是否由 MVC 框架管理 测试
  • 在分派事件时框架是否调用 LoginCommand。

在其他容器/框架环境中,我倾向于不编写行使框架职责的测试,因为(恕我直言)这会导致脆弱的测试。然而,由于缺乏编译器检查,在这种情况下它似乎是有道理的。

想法?

I'm writing an application using an MVC framework which takes care of a lot of the boilerplate wiring of our system. Specifically - application is written in Flex, using the Parsley MVC framework. However, the question is not language specific.

In my Presentation Model / Code-Behind / View-Controller (whatever you want to call it), I might have something like this:

[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));
   }
 }

Then, elsewhere in my system, code which responds to this, would look like this

public class LoginCommand {
   [MessageHandler]
   public function execute(attemptLoginEvent:AttemptLoginEvent):void {
      // Do login related stuff
   }
}

It's important to note that within Flex / Actionscript, Metatags are not checked by the compiler. For example:

[Event(name="attemptLogin",type="com.foo.AttemptLoginEvent")]
[ManagedEvent["attemptLogin"] // Spelling mistake - metatag is ManagedEvents
public class LoginViewPM {

and

[Event(name="attemptLogin",type="com.foo.AttemptLoginEvent")]
[ManagedEvent["attemtLogin"] // Spelling mistake - event name is wrong
public class LoginViewPM {

In the above two examples, the framework will fail. In the first example it fails silently (because the metatag is incorrect - hence the framework is never engaged). In the second example, we get some runtime logging that partially alerts us that things are wrong.

Given this, what is a pragmatic level of unit testing for the attemptLogin() method on the PM, with regard to the MVC framework's duties? Ie:

Should I:

  • Test that the AttemptLoginEvent is managed by the MVC framework
  • Test that the LoginCommand gets invoked by the framework when the event is dispatched.

In other container / framework environments, I tend not to write tests that exercise the responsibilities of the frameworks, as (IMHO) this leads to brittle tests. However, given the lack of compiler checking, in this case it may seem warrented.

Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无敌元气妹 2024-08-27 08:26:36

如果您考虑一下,您实际上并没有测试框架的职责,而是测试编码员的打字能力。

但是,如果编写事件的同一编码器也编写测试,并且事件名称不会经常更新,那么您可能会跳过它,因为在编写测试时很可能会捕获任何拼写错误。

If you think about it, you're not really testing the responsibilities of the framework as much as you are testing how well your coders can type.

However, if the same coder who wrote the event also writes the test, and if the event name is something that will not be updated frequently, then you could probably skip it, since any typos would most likely be caught while the test is being written.

無心 2024-08-27 08:26:36

您想要测试的内容听起来像是集成测试。如果您想要进行单元测试,则必须定义您的单元是什么。

如果您想测试您的事件,请模拟事件接收器并查看随后是否调用了模拟。

public class MockLoginCommand : ICommandReceiver {

   public bool BeenCalled { get; set; }

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

ETC。

What you want to test sounds like integration testing. If you want a unit test, you have to define what your unit is.

If you want to test your eventing, mock the event receiver and find out whether the mock has been called afterwards.

public class MockLoginCommand : ICommandReceiver {

   public bool BeenCalled { get; set; }

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

etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文