如何对心跳模式进行单元测试?
我正在编写一个客户端,它每秒向服务器发送一个“心跳信号”。客户端在后台线程中调用 WCF 服务来报告其活动。
如何对此进行单元测试?我是否需要等待几秒钟并检查是否多次调用了适当的方法?
应该是某种场景测试吗?也许我不应该担心在整个客户端生命周期中不断调用服务?
我可以测试对 WCF 服务的单个调用,但它不会测试“心跳模式”。
我正在使用 TDD 方法。 (C#、NUnit、Moq)
有什么建议或示例吗?
编辑:
我认为这还不够清楚。
这是我所拥有的更简单的版本:
public class FeedService
{
private Timer t;
public FeedService()
{
t.Interval = 1000;
t.Elapsed += TimerElapsed;
t.Start();
}
private void TimerElapsed(object sender, ElapsedEventArgs e)
{
t.Stop();
SendHeartbeat();
t.Start();
}
}
...这是我的测试:
[Test]
public void Heartbeat_called_twice_after_2_seconds()
{
var mockFeedService = new Mock<FeedService>();
Thread.Sleep(2000);
mockFeedService.Verify(x => x.SendHeartBeat(), Times.AtLeast(2));
}
我有两个问题:
1)为什么我的测试总是失败?我做错了什么?
2)我应该测试它吗?
I am writing a client which sends a "heartbeat signal" to the server every second. The client calls a WCF service in the background thread to report its activity.
How to unit test this? Do I need to wait for a couple of seconds and check if appropriate method was called several times?
Should it be some sort of scenario test instead? Maybe I shouldn't be worried about calling the service continuously through the whole clients lifetime cycle?
I can test a single call to WCF service but it doesn't test the "heartbeat pattern".
I am using TDD approach. (C#, NUnit, Moq)
Any suggestions or examples?
EDIT:
I think it wasn't clear enough.
This is a much simpler version of what I have:
public class FeedService
{
private Timer t;
public FeedService()
{
t.Interval = 1000;
t.Elapsed += TimerElapsed;
t.Start();
}
private void TimerElapsed(object sender, ElapsedEventArgs e)
{
t.Stop();
SendHeartbeat();
t.Start();
}
}
...and this is my test:
[Test]
public void Heartbeat_called_twice_after_2_seconds()
{
var mockFeedService = new Mock<FeedService>();
Thread.Sleep(2000);
mockFeedService.Verify(x => x.SendHeartBeat(), Times.AtLeast(2));
}
I have a two questions:
1) Why my test always fails? What I am doing wrong?
2) Should I test it at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
必须首先隔离您想要测试的功能。例如在本例中,有两个方面可以测试。一是心跳组件实际上按照指定的时间表发送心跳消息。另一个是服务接收消息。如果您抽象服务,那么您可以独立于服务实现来测试心跳组件。这可以在单元测试中完成,方法是启动心跳组件,然后休眠,然后验证存根或模拟服务实现是否收到了预期数量的消息。确保服务接收消息的测试是集成测试,因此不是“纯”单元测试。但是,由于无论如何都必须对其进行测试,因此您也可以为此提供一个测试用例。
The functionality that you wish to test must be first isolated. For example in this case, there are a two aspects that could be tested. One is that the hearbeat component actually sends a heartbeat message on the designated schedule. The other is that the service receives the message. If you abstract the service, then you can test the hearbeat component in isolation from the service implementation. This can be done in a unit test by starting the hearbeat component, then sleeping, then verifying that the expected number of messages were received by the stub or mock service implementation. The test that ensures the service is receiving the message is an integration test and so is a not a "pure" unit test. However, since it must be tested anyway, you can have a test case for this as well.
我会将与服务的交互包装在“网关”类型的类中。这可以替换为测试中的模拟,并且模拟可以计算您所做的调用或您可能需要的任何统计数据。
问候,
莫滕
I would wrap the interaction with the service in a "gateway" type class. This can be replaced by a mock in your test, and the mock can count the calls that you make, or whatever statistics you might need.
Regards,
Morten
严格来说,您要测试的场景是集成测试。
我对类似测试构建场景采取的方法是使用这个方便的函数:
用法:
它使用自 .Net4 以来才可用的 c# 功能,但这可能适合当今大多数 .Net 开发人员。
The scenario you want to test is, strictly speaking, an integration test.
The approach I've taken to similar test-construction scenarios is to use this handy function:
Usage:
It uses c# features only available since .Net4 but that probably suits most .Net developers these days.