简体   繁体   English

单元测试HTTP请求休息API

[英]Unit testing http requests to rest api

I have a C# library that gets data from public api. 我有一个C#库,可从公共api获取数据。 trying to get into TDD and was wondering how to unit test a library that the main focus of it is to get data from the server and convert the json to .net objects. 试图进入TDD,并且想知道如何对库进行单元测试,该库的主要焦点是从服务器获取数据并将json转换为.net对象。 (Basically handling the http requests along with limiting and error handling and some configuration settings) (基本处理http请求以及限制和错误处理以及一些配置设置)
I know you should mock some sort of database but not sure how exactly. 我知道您应该模拟某种数据库,但不确定具体如何。 And i've read that the tests should run all the time even with internet connection off. 而且我读到即使关闭Internet连接,测试也应该一直运行。

You can implement only integration testing. 您只能实施集成测试。 You also can test your code with internet connection off - just create a kind of wrapper. 您也可以在不连接互联网的情况下测试您的代码-只需创建一种包装即可。 Among various design patterns the most appropriate is Facade for this kind of task. 在各种设计模式中,最合适的是Facade来完成此类任务。 Create Facade around third-party library so you can mock this facade in future and make it produce desired/undesired results so you can unit test your classes. 围绕第三方库创建Facade,以便将来可以模拟此Facade,并使其产生所需/不需要的结果,从而可以对类进行单元测试。 But: Don't test or mock what you don't own 但是: 不要测试或嘲笑您不拥有的东西

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

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