2 tdd unit-testing client-server protocols network-protocols
目前我正在创建一个服务器应用程序来接收特定于协议的消息.我需要创建测试以确保我已正确实现协议.这是某种集成测试吗?如果是肯定的,我可以使用单元测试工具进行集成测试吗?最后,创建这类测试的最佳方法是什么?
如果您知道正确的答案是什么,那么这就是我要做的事情:
将负责处理协议逻辑的类与处理连接机制的代码分开.
一次编写一个测试,为给定的一组输入消息指定正确的响应.
实施这些行为.
例如,如果应该使用"howdy"消息响应"hello"消息,则您的测试可能如下所示:
Mock<IProtocolOut> outbound = new Mock<IProtocolOut>();
MyProtocolHandler handler = new MyProtocolHandler(outbound); // assuming that the handler takes the outbound receiver as a parameter.
outbound.Expect(o=>o.HowdyMessage()); // we expect to get a HowdyMessage back
handler.HelloMessage(); // 'fake' a HelloMessage into the handler
outbound.VerifyAll(); // assert that the 'howdy' message was sent.
Run Code Online (Sandbox Code Playgroud)
在这种情况下,所有模拟都声称已经进行了某些调用.这可以通过手工操作类来完成验证 - 模拟没有什么神奇之处,它们只是更容易进行这种类型的验证.
如果你有一个支持Arrange/Act/Assert的模拟库,它看起来更像是这样的:
Mock<IProtocolOut> outbound = new Mock<IProtocolOut>();
MyProtocolHandler handler = new MyProtocolHandler(outbound); // assuming that the handler takes the outbound receiver as a parameter.
handler.HelloMessage(); // fake the message being sent
outbound.AssertWasCalled(o=>o.HowdyMessage());
Run Code Online (Sandbox Code Playgroud)
当然,协议的接口不必用消息强类型化.你也可以做类似的事情:
Mock<IProtocolOut> outbound = new Mock<IProtocolOut>();
MyProtocolHandler handler = new MyProtocolHandler(outbound); // assuming that the handler takes the outbound receiver as a parameter.
handler..ReceiveMessage("hello"); // fake the message being sent
outbound.AssertWasCalled(o=>o.ReceiveMessage("howdy"));
Run Code Online (Sandbox Code Playgroud)
(编辑以澄清测试范围)
这些都不需要"实际"连接.它们仅测试处理协议的逻辑方面,并假设您在逻辑协议处理和连接管理之间进行了划分.
| 归档时间: |
|
| 查看次数: |
2263 次 |
| 最近记录: |