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?
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.