简体   繁体   English

编写具有网络连接依赖性的方法的单元测试

[英]Writing unit test for methods which have network connection dependencies

I am writing a Downloader (for fun with TDD), in that i have a method, whose responsibility is to connect to the file. 我正在编写一个Downloader(用于TDD娱乐),因为我有一个方法,其职责是连接到文件。

class FileConnectorHttp : IFileConnector
{
public void ConnectToFile()
{
  //creating a concrete web request here.
  HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(UriForTheFileToConnect);

  //sending HEAD request.
  webRequest.Method = "HEAD";

  //other logic for connection will go here.
  try
  {
    HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse;

  }
  catch (ProtocolViolationException e)
  {
    throw;
  }
  catch (InvalidOperationException e)
  {

    throw;
  }

}
}

I wrote a test for testing the scenario of connection timeout. 我编写了一个测试连接超时情况的测试。

[Test]
public void ConnectToSourceFile_ValidUri_Connection_TimeOut_Throws_WebException()
{
  Uri uriForTheFileToDownload = new Uri("https://suppose/this/is/valid/url.txt");

  FileConnectorHttp fileConnectorOverHttp = new FileConnectorHttp(uriForTheFileToDownload);

  Assert.Throws(Is.TypeOf<WebException>(),

    () => fileConnectorOverHttp.ConnectToFile());

}

So to get this exception should i turn off my Wifi connection? 因此,要获得此例外,我应该关闭Wifi连接吗? What should be the meaningful way to test for this kind of exception? 测试这种异常的有意义的方法应该是什么?

There is no reason for you to test that a failure to connect throws a WebException. 您没有理由测试连接失败会引发WebException。 It's not your code that is doing the throwing. 引发错误的不是您的代码。

OTOH, you probably want to test at some point that your code properly handles a WebException when it is thrown . OTOH,您可能希望在某些时候测试您的代码在抛出 WebException 时可以正确处理 To do that, you will have to wrap or mock the underlying web request. 为此,您必须包装或模拟基础Web请求。

A Unit Test ideally should not rely on external dependancies like Network connections. 理想情况下,单元测试不应依赖于诸如网络连接之类的外部依赖关系。 As mentioned in the comment above (the link) you want to Mock this behaviour. 如上面评论(链接)所述,您想模拟这种行为。 Otherwise using a real connection you would not be able to test failure cases ie A good connection and no connection in an automated way. 否则,使用真实连接将无法测试故障情况,即良好连接和无连接会自动进行。

Unit Tests should follow the FIRST acronym 单元测试应遵循FIRST首字母缩写

  • Fast 快速
  • Independent 独立
  • Repeatable 重复
  • Self-Validating 自确认
  • Timely 及时

If you need a test to actually use the physically connection, then you could write an Integration or System test - Maybe using BDD. 如果您需要测试以实际使用物理连接,则可以编写集成测试或系统测试-也许使用BDD。

The benefits to this approach is that your unit test suite can run quickly (automated), cover many scenario's and give you very quick feedback. 这种方法的好处是您的单元测试套件可以快速(自动)运行,涵盖许多场景,并且可以为您提供快速反馈。 Whereas the Integration Test can be run less frequently (if desired) and you need not worry so much about how long they take, as all you are doing here is proving end-to-end connectivity. 集成测试的运行频率可能较低(如果需要),并且您不必担心它们花费的时间,因为您在这里所做的所有工作就是证明端到端的连接性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM