单元测试计时器的应用程序? [英] Unit testing a timer based application?

查看:175
本文介绍了单元测试计时器的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写在C#中执行一个动作n次每k秒钟一个简单的,基于定时器的迷你应用程序。
我想通过一个测试驱动的开发风格,所以我的目标是单元测试应用程序的所有部分。

I am currently writing a simple, timer based mini app in C# that performs an action n times every k seconds.
I am trying to adopt a test driven development style, so my goal is to unit test all parts of the app.

所以,我的问题是:有没有一种很好的方式进行单元测试计时器基础类

So, my question is: Is there a good way to unit test a timer based class?

这个问题,在我看来,是有很大的风险,该测试将采取不舒服的长来执行,因为他们必须等待某某长所需行动的情况发生。
特别是如果一个人想真实数据,而不是使用由所述框架所允许的最小的时间分辨率(秒),(1毫秒?)。
我使用的是模拟对象的动作,来注册的动作被称为的次数,因此该操作需要几乎没有时间。

The problem, as I see it, is that there is a big risk that the tests will take uncomfortably long to execute, since they must wait so and so long for the desired actions to happen.
Especially if one wants realistic data (seconds), instead of using the minimal time resolution allowed by the framework (1 ms?).
I am using a mock object for the action, to register the number of times the action was called, and so that the action takes practically no time.

推荐答案

我所做的是模拟定时器,也是当前系统时间,我的事件可以立即生效,但至于$ C $引发根据测试C为经过的关注时间为秒。

What I have done is to mock the timer, and also the current system time, that my events could be triggered immediately, but as far as the code under test was concerned time elapsed was seconds.

这篇关于单元测试计时器的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆